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

Handle JSON parse errors to prevent watch from crashing if a non-JSON request body is received. #216

Merged
merged 1 commit into from
Oct 6, 2022

Conversation

brianpursley
Copy link
Contributor

The problem

When using lambda-local in --watch mode, if you post and empty body or body containing invalid JSON, the lambda-local process crashes.

Example output when posting empty body:

info: Lambda handler listening on http://localhost:8008
undefined:1


SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at IncomingMessage.<anonymous> (C:\Users\lc1211766\Projects\eVault\src\api\node_modules\lambda-local\build\lambdalocal.js:159:28)
    at IncomingMessage.emit (events.js:412:35)
    at endReadableNT (internal/streams/readable.js:1317:12)
    at processTicksAndRejections (internal/process/task_queues.js:82:21)

(lambda-local process crashes after this error occurs)

Example output when posting body that is not valid JSON:

info: Lambda handler listening on http://localhost:8008
undefined:1
x
^

SyntaxError: Unexpected token x in JSON at position 0
    at JSON.parse (<anonymous>)
    at IncomingMessage.<anonymous> (C:\Users\lc1211766\Projects\eVault\src\api\node_modules\lambda-local\build\lambdalocal.js:159:28)
    at IncomingMessage.emit (events.js:412:35)
    at endReadableNT (internal/streams/readable.js:1317:12)
    at processTicksAndRejections (internal/process/task_queues.js:82:21)

(lambda-local process crashes after this error occurs)

How this PR fixes the problem

This PR adds a try/catch around the JSON.parse(body) call to handle exceptions that can occur while parsing the body and returning them through the callback.

(It also adds a return after the callback for missing payload.event so that it doesn't trigger multiple callbacks in that case)

@jamesliupenn
Copy link

@brianpursley do you recall why was this closed?

@gpotter2 gpotter2 reopened this Oct 6, 2022
@brianpursley
Copy link
Contributor Author

@brianpursley do you recall why was this closed?

Not sure. We stopped using lambda-local (we switched to serverless). I probably just closed it because it sat open for months without any comment and I figured it wasn't something that would ever get merged.

I think it is probably a good change if they want to accept it though.

@gpotter2 gpotter2 merged commit 48630c8 into ashiina:develop Oct 6, 2022
@gpotter2
Copy link
Collaborator

gpotter2 commented Oct 6, 2022

Yeah the change is fine, sorry about that. I think I missed this PR :/

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.

None yet

3 participants