Skip to content

Commit 11ec075

Browse files
authored
Treat errors on handling income bolt messages (#957)
Invalid income messages which could not be parsed or handled correctly where throwing exceptions in non-safe context. This leads the driver to crash hard since the error is bubble up to the socket thread.
1 parent 89a8972 commit 11ec075

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

packages/bolt-connection/src/bolt/create.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ export default function create ({
7070

7171
// setup dechunker to dechunk messages and forward them to the message handler
7272
dechunker.onmessage = buf => {
73-
responseHandler.handleResponse(protocol.unpacker().unpack(buf))
73+
try {
74+
responseHandler.handleResponse(protocol.unpacker().unpack(buf))
75+
} catch (e) {
76+
return observer.onError(e)
77+
}
7478
}
7579

7680
return responseHandler

packages/bolt-connection/test/bolt/index.test.js

+59
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,65 @@ describe('#unit Bolt', () => {
275275
expect(receivedMessage.signature).toEqual(expectedMessage.signature)
276276
expect(receivedMessage.fields).toEqual(expectedMessage.fields)
277277
})
278+
279+
it(`it should configure the channel.onmessage to dechunk and notify unpacking issues ${version}`, () => {
280+
const params = createBoltCreateParams({ version })
281+
let receivedMessage = null
282+
const message = {
283+
signature: 0x10,
284+
fields: [123]
285+
}
286+
const protocol = Bolt.create(params)
287+
protocol._responseHandler.handleResponse = msg => {
288+
receivedMessage = msg
289+
}
290+
291+
protocol.packer().packStruct(
292+
message.signature,
293+
message.fields.map(field => protocol.packer().packable(field))
294+
)
295+
296+
params.chunker.messageBoundary()
297+
params.chunker.flush()
298+
299+
const expectedError = newError('Something went wrong')
300+
protocol.unpacker = jest.fn(() => {
301+
return {
302+
unpack: () => {
303+
throw expectedError
304+
}
305+
}
306+
})
307+
308+
params.channel.onmessage(params.channel.toBuffer())
309+
310+
expect(receivedMessage).toBeNull()
311+
expect(params.observer.errors).toEqual([expectedError])
312+
})
313+
314+
it(`it should configure the channel.onmessage to dechunk and notify response handler issues ${version}`, () => {
315+
const params = createBoltCreateParams({ version })
316+
let receivedMessage = null
317+
const expectedMessage = {
318+
signature: 0x10,
319+
fields: [123]
320+
}
321+
const protocol = Bolt.create(params)
322+
const expectedError = newError('Something went wrong')
323+
protocol._responseHandler.handleResponse = msg => {
324+
throw expectedError
325+
}
326+
327+
protocol.packer().packStruct(
328+
expectedMessage.signature,
329+
expectedMessage.fields.map(field => protocol.packer().packable(field))
330+
)
331+
params.chunker.messageBoundary()
332+
params.chunker.flush()
333+
params.channel.onmessage(params.channel.toBuffer())
334+
335+
expect(params.observer.errors).toEqual([expectedError])
336+
})
278337
})
279338

280339
forEachUnknownProtocolVersion(version => {

0 commit comments

Comments
 (0)