diff --git a/packages/bolt-connection/src/bolt/create.js b/packages/bolt-connection/src/bolt/create.js index a8ab0d0d6..358b63977 100644 --- a/packages/bolt-connection/src/bolt/create.js +++ b/packages/bolt-connection/src/bolt/create.js @@ -70,7 +70,11 @@ export default function create ({ // setup dechunker to dechunk messages and forward them to the message handler dechunker.onmessage = buf => { - responseHandler.handleResponse(protocol.unpacker().unpack(buf)) + try { + responseHandler.handleResponse(protocol.unpacker().unpack(buf)) + } catch (e) { + return observer.onError(e) + } } return responseHandler diff --git a/packages/bolt-connection/test/bolt/index.test.js b/packages/bolt-connection/test/bolt/index.test.js index 838b46be6..2b9901df6 100644 --- a/packages/bolt-connection/test/bolt/index.test.js +++ b/packages/bolt-connection/test/bolt/index.test.js @@ -275,6 +275,65 @@ describe('#unit Bolt', () => { expect(receivedMessage.signature).toEqual(expectedMessage.signature) expect(receivedMessage.fields).toEqual(expectedMessage.fields) }) + + it(`it should configure the channel.onmessage to dechunk and notify unpacking issues ${version}`, () => { + const params = createBoltCreateParams({ version }) + let receivedMessage = null + const message = { + signature: 0x10, + fields: [123] + } + const protocol = Bolt.create(params) + protocol._responseHandler.handleResponse = msg => { + receivedMessage = msg + } + + protocol.packer().packStruct( + message.signature, + message.fields.map(field => protocol.packer().packable(field)) + ) + + params.chunker.messageBoundary() + params.chunker.flush() + + const expectedError = newError('Something went wrong') + protocol.unpacker = jest.fn(() => { + return { + unpack: () => { + throw expectedError + } + } + }) + + params.channel.onmessage(params.channel.toBuffer()) + + expect(receivedMessage).toBeNull() + expect(params.observer.errors).toEqual([expectedError]) + }) + + it(`it should configure the channel.onmessage to dechunk and notify response handler issues ${version}`, () => { + const params = createBoltCreateParams({ version }) + let receivedMessage = null + const expectedMessage = { + signature: 0x10, + fields: [123] + } + const protocol = Bolt.create(params) + const expectedError = newError('Something went wrong') + protocol._responseHandler.handleResponse = msg => { + throw expectedError + } + + protocol.packer().packStruct( + expectedMessage.signature, + expectedMessage.fields.map(field => protocol.packer().packable(field)) + ) + params.chunker.messageBoundary() + params.chunker.flush() + params.channel.onmessage(params.channel.toBuffer()) + + expect(params.observer.errors).toEqual([expectedError]) + }) }) forEachUnknownProtocolVersion(version => {