Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions server/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

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.

const isProduction = process.env.NODE_ENV === 'production';
...
message: !isProduction ? err.message : undefined

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error Message Detail

The 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:
Enhance the error handling by including more specific error descriptions based on the exception caught, while still protecting sensitive information in production environments.

if (!isProduction) {
  console.error('Detailed error:', err);
}
res.status(500).json({
  error: 'Server error',
  message: !isProduction ? 'Detailed error information here' : undefined
});

});
}
});

Expand All @@ -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
});
}
});

Expand All @@ -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
});
}
});

Expand All @@ -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
});
}
});

Expand All @@ -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
});
}
});

Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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';

});
}
});

Expand All @@ -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
});
}
});

Expand Down Expand Up @@ -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'
});
}
});
Expand All @@ -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'
});
}
});
Expand Down Expand Up @@ -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'
});
}
});
Expand Down