Skip to content

Improve API error reporting#58

Open
numbpill3d wants to merge 1 commit intomainfrom
codex/modify-error-handlers-in-api.js
Open

Improve API error reporting#58
numbpill3d wants to merge 1 commit intomainfrom
codex/modify-error-handlers-in-api.js

Conversation

@numbpill3d
Copy link
Collaborator

@numbpill3d numbpill3d commented Jun 8, 2025

Summary

  • show error.message when API errors occur and not in production
  • adjust streetpass routes to use NODE_ENV !== 'production'

Testing

  • npm test (fails: Missing required Supabase env variables)

https://chatgpt.com/codex/tasks/task_e_68450e28c0ac832fb1008eabbce0442d

Summary by Sourcery

Improve API error reporting by exposing detailed error messages in non-production environments and standardize environment checks for streetpass routes.

Enhancements:

  • Augment all server API error responses to include the original error message when NODE_ENV is not production
  • Replace development-only checks in streetpass routes with a unified production check (NODE_ENV !== 'production') for error handling

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jun 8, 2025

Reviewer's Guide

This PR enhances API error handling by conditionally exposing detailed error messages in JSON responses outside of production and updates streetpass routes to use a NODE_ENV !== 'production' check instead of a development-only check.

Class Diagram for Updated Generic API Error Response Structure

classDiagram
    class GenericApiErrorResponse {
      +String error
      +String message?
    }
    note "The 'message' field is populated with detailed error information (err.message) if process.env.NODE_ENV !== 'production'. Otherwise, it may be undefined or omitted."
Loading

File-Level Changes

Change Details Files
Include detailed error messages in API error responses outside production
  • Replace generic 500 responses in user endpoints to include err.message conditionally
  • Apply the same enhancement to scrapyard endpoints
  • Apply the same enhancement to marketplace endpoints
server/routes/api.js
Use NODE_ENV !== 'production' for streetpass route error conditions
  • Change streetpass visit error response to check for non-production
  • Change streetpass visitors endpoint error response to non-production check
  • Change streetpass emote endpoint error response to non-production check
server/routes/api.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @numbpill3d - I've reviewed your changes - here's some feedback:

  • Extract the repeated error response logic into a shared helper or middleware so you’re not duplicating the same JSON formatting in every route.
  • Double-check that using NODE_ENV !== 'production' won’t leak sensitive error details in your staging or test environments—consider a more explicit allowlist if needed.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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

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

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.

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
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant