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

Null RPC responses throw JSON parse error #285

Closed
ChrisBates opened this issue Jul 2, 2021 · 1 comment · Fixed by #286
Closed

Null RPC responses throw JSON parse error #285

ChrisBates opened this issue Jul 2, 2021 · 1 comment · Fixed by #286

Comments

@ChrisBates
Copy link
Contributor

In initDirectReplyQueue() (connection.js) it's trying to parse what it expects to be JSON content inside the Buffer.

JSON.parse(msg.content.toString()),

If the Buffer is empty (i.e. the RPC handler returned a null/void/undefined) then it's trying to parse an empty string. This throws an error.

(node:2736) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at channel.consume.noAck (/usr/local/src/packages/wilco-svc-omniapi/node_modules/@golevelup/nestjs-rabbitmq/lib/amqp/connection.js:95:67)
    at ConfirmChannel.BaseChannel.dispatchMessage (/usr/local/src/packages/wilco-svc-omniapi/node_modules/amqplib/lib/channel.js:484:12)
    at ConfirmChannel.BaseChannel.handleDelivery (/usr/local/src/packages/wilco-svc-omniapi/node_modules/amqplib/lib/channel.js:493:15)
    at ConfirmChannel.emit (events.js:314:20)
    at /usr/local/src/packages/wilco-svc-omniapi/node_modules/amqplib/lib/channel.js:279:10
    at ConfirmChannel.headers (/usr/local/src/packages/wilco-svc-omniapi/node_modules/amqplib/lib/channel.js:306:9)
    at ConfirmChannel.C.acceptMessageFrame (/usr/local/src/packages/wilco-svc-omniapi/node_modules/amqplib/lib/channel.js:247:31)
    at ConfirmChannel.C.accept (/usr/local/src/packages/wilco-svc-omniapi/node_modules/amqplib/lib/channel.js:400:17)
    at Connection.mainAccept [as accept] (/usr/local/src/packages/wilco-svc-omniapi/node_modules/amqplib/lib/connection.js:64:33)
    at Socket.go (/usr/local/src/packages/wilco-svc-omniapi/node_modules/amqplib/lib/connection.js:478:48)
    at Socket.emit (events.js:314:20)
    at emitReadable_ (_stream_readable.js:557:12)
    at processTicksAndRejections (internal/process/task_queues.js:83:21)

I was going to propose handling it the same way it's currently being handled in handleMessage() with a try/catch, this seems robust but would also resolve non-JSON content as undefined (not just null values) which may be undesirable.

Instead I propose more specifically handling an empty Buffer to prevent JSON parse from throwing the error. Will submit a PR.

@WonderPanda
Copy link
Collaborator

@ChrisBates Thanks for the contribution. This is available as of @golevelup/nestjs-rabbitmq@1.17.1

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 a pull request may close this issue.

2 participants