Skip to content
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

feat: json parsing error #2042

Merged
merged 3 commits into from
Oct 31, 2022
Merged

feat: json parsing error #2042

merged 3 commits into from
Oct 31, 2022

Conversation

developerdenesh
Copy link
Contributor

Problem

What problem are you trying to solve? What issue does this close?

Closes #1961

Solution

How did you solve the problem?
Check the incoming error code, type and syntax error. If 400 is received, return 400 back

Features:

  • Details ...

Improvements:

  • Details ...

Bug Fixes:

  • Details ...

Before & After Screenshots

BEFORE:
[insert screenshot here]

AFTER:
[insert screenshot here]

Tests

What tests should be run to confirm functionality?

Deploy Notes

Notes regarding deployment of the contained body of work. These should note any
new dependencies, new scripts, etc.

New environment variables:

  • env var : env var details

New scripts:

  • script : script details

New dependencies:

  • dependency : dependency details

New dev dependencies:

  • dependency : dependency details

@developerdenesh
Copy link
Contributor Author

This takes over from #2030

Comment on lines 228 to 231
err instanceof SyntaxError &&
err.statusCode === 400 &&
'body' in err &&
err.type === 'entity.parse.failed'
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for contributing to our code!

This error message is specific to the body-parser library we use. I suggest that it'd be nice for future reference either to 1) leave an inline comment above to note that this catches body-parser errors and returns a 400 error message (similar like how we did it with Joi errors above), or 2) wrap an error handler around the bodyParser.json() parsing in apiMiddleware like so, so that it's clear what this piece of code is meant to be catching.

(req, res, next) => {
    bodyParser.json()(req, res, err => {
        if (err) {
            console.error(err);
            return res.sendStatus(400); // Bad request
        }

        next();
    });

Taken from https://stackoverflow.com/questions/53048642/node-js-handle-body-parser-invalid-json-error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @gweiying, have added a comment similar to the Joi errors to reflect that this catches body-parser json errors and returns error 400 accordingly

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the quick fix.

Pulled your code locally to run - just a couple of small issues.

  1. missing return statement
  2. small issue with Typescript - since status and type does not exist on SyntaxError, the code cannot be compiled. A quick solution for this is to just drop the err instanceof SyntaxError check since the other errors should be specific enough to catch the body parser error, or possibly to create our own error type - the latter might be more involved.

Copy link
Contributor

@gweiying gweiying left a comment

Choose a reason for hiding this comment

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

lgtm, thanks for your contribution

@halfwhole halfwhole merged commit 26eb009 into opengovsg:develop Oct 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle 500 error with invalid JSON input
3 participants