-
Notifications
You must be signed in to change notification settings - Fork 0
Improve API error reporting #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,10 @@ router.get('/users', async (req, res) => { | |
| res.json(users); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: 'Server error' }); | ||
| res.status(500).json({ | ||
| error: 'Server error', | ||
| message: process.env.NODE_ENV !== 'production' ? err.message : undefined | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error Message DetailThe error messages returned from the server are generic ('Server error') without much detail about what went wrong, which can hinder debugging and user feedback. While it's good practice not to expose detailed error information in production, during development, more informative messages could be beneficial. Recommendation: if (!isProduction) {
console.error('Detailed error:', err);
}
res.status(500).json({
error: 'Server error',
message: !isProduction ? 'Detailed error information here' : undefined
}); |
||
| }); | ||
| } | ||
| }); | ||
|
|
||
|
|
@@ -40,7 +43,10 @@ router.get('/users/:username', async (req, res) => { | |
| res.json(user); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: 'Server error' }); | ||
| res.status(500).json({ | ||
| error: 'Server error', | ||
| message: process.env.NODE_ENV !== 'production' ? err.message : undefined | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
|
|
@@ -56,7 +62,10 @@ router.get('/me', ensureAuthenticated, async (req, res) => { | |
| res.json(user); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: 'Server error' }); | ||
| res.status(500).json({ | ||
| error: 'Server error', | ||
| message: process.env.NODE_ENV !== 'production' ? err.message : undefined | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
|
|
@@ -74,7 +83,10 @@ router.get('/scrapyard', async (req, res) => { | |
| res.json(items); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: 'Server error' }); | ||
| res.status(500).json({ | ||
| error: 'Server error', | ||
| message: process.env.NODE_ENV !== 'production' ? err.message : undefined | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
|
|
@@ -91,7 +103,10 @@ router.get('/scrapyard/:id', async (req, res) => { | |
| res.json(item); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: 'Server error' }); | ||
| res.status(500).json({ | ||
| error: 'Server error', | ||
| message: process.env.NODE_ENV !== 'production' ? err.message : undefined | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
|
|
@@ -135,7 +150,10 @@ router.get('/market/items', async (req, res) => { | |
| }); | ||
| } catch (error) { | ||
| console.error('Error fetching market items:', error); | ||
| res.status(500).json({ error: 'An error occurred while fetching marketplace items' }); | ||
| res.status(500).json({ | ||
| error: 'An error occurred while fetching marketplace items', | ||
| message: process.env.NODE_ENV !== 'production' ? error.message : undefined | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Extract environment check into a constant Defining a top-level isProduction constant will make these environment checks clearer and reduce repetition. Suggested implementation: res.status(500).json({
error: 'An error occurred while fetching marketplace items',
message: !isProduction ? error.message : undefined
}); res.status(500).json({
error: 'Server error',
message: !isProduction ? err.message : undefined
}); });
}
});
// Top-level environment check constant
const isProduction = process.env.NODE_ENV === 'production'; |
||
| }); | ||
| } | ||
| }); | ||
|
|
||
|
|
@@ -151,7 +169,10 @@ router.get('/market/items/:id', async (req, res) => { | |
| res.json(item); | ||
| } catch (error) { | ||
| console.error('Error fetching market item:', error); | ||
| res.status(500).json({ error: 'An error occurred while fetching the marketplace item' }); | ||
| res.status(500).json({ | ||
| error: 'An error occurred while fetching the marketplace item', | ||
| message: process.env.NODE_ENV !== 'production' ? error.message : undefined | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
|
|
@@ -182,7 +203,7 @@ router.post('/streetpass/visit', ensureAuthenticated, async (req, res) => { | |
| res.status(500).json({ | ||
| success: false, | ||
| message: 'Failed to record visit', | ||
| error: process.env.NODE_ENV === 'development' ? error.message : 'Server error' | ||
| error: process.env.NODE_ENV !== 'production' ? error.message : 'Server error' | ||
| }); | ||
| } | ||
| }); | ||
|
|
@@ -208,7 +229,7 @@ router.get('/streetpass/visitors/:profileId', async (req, res) => { | |
| res.status(500).json({ | ||
| success: false, | ||
| message: 'Failed to get visitors', | ||
| error: process.env.NODE_ENV === 'development' ? error.message : 'Server error' | ||
| error: process.env.NODE_ENV !== 'production' ? error.message : 'Server error' | ||
| }); | ||
| } | ||
| }); | ||
|
|
@@ -237,7 +258,7 @@ router.put('/streetpass/emote', ensureAuthenticated, async (req, res) => { | |
| res.status(500).json({ | ||
| success: false, | ||
| message: 'Failed to update emote', | ||
| error: process.env.NODE_ENV === 'development' ? error.message : 'Server error' | ||
| error: process.env.NODE_ENV !== 'production' ? error.message : 'Server error' | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant Environment Check
The environment check
process.env.NODE_ENV !== 'production'is used multiple times across different endpoints to conditionally display error messages. This could be optimized by defining a constant at the top of the file to handle this logic, reducing redundancy and improving maintainability.Recommendation:
Define a constant at the beginning of your file and use it to replace all instances of this check.