From 723a9c556eb8d8f395b49f7282ce52d037c1e44e Mon Sep 17 00:00:00 2001 From: Antonio Barcelos Date: Tue, 9 Nov 2021 15:11:17 +0100 Subject: [PATCH] Improve error logging during ChannelConnection._handleFatalError --- .../src/connection/connection-channel.js | 2 +- .../connection/connection-channel.test.js | 32 ++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/bolt-connection/src/connection/connection-channel.js b/packages/bolt-connection/src/connection/connection-channel.js index e1559b1c8..b72cecb8d 100644 --- a/packages/bolt-connection/src/connection/connection-channel.js +++ b/packages/bolt-connection/src/connection/connection-channel.js @@ -277,7 +277,7 @@ export default class ChannelConnection extends Connection { if (this._log.isErrorEnabled()) { this._log.error( - `experienced a fatal error ${json.stringify(this._error)}` + `experienced a fatal error caused by ${this._error} (${json.stringify(this._error)})` ) } diff --git a/packages/bolt-connection/test/connection/connection-channel.test.js b/packages/bolt-connection/test/connection/connection-channel.test.js index 9b01a19ed..f5ffbe46d 100644 --- a/packages/bolt-connection/test/connection/connection-channel.test.js +++ b/packages/bolt-connection/test/connection/connection-channel.test.js @@ -129,6 +129,7 @@ describe('ChannelConnection', () => { describe('._handleFatalError()', () => { describe('when there is not current failure on going', () => { const thrownError = newError('some error', 'C') + let loggerFunction; let notifyFatalError; let connection; @@ -138,9 +139,10 @@ describe('ChannelConnection', () => { notifyFatalError, currentFailure: null } - + loggerFunction = jest.fn() + const logger = new Logger('info', loggerFunction) const protocolSupplier = () => protocol - connection = spyOnConnectionChannel({ protocolSupplier }) + connection = spyOnConnectionChannel({ protocolSupplier, logger }) }) it('should set connection state to broken', () => { @@ -160,11 +162,22 @@ describe('ChannelConnection', () => { expect(notifyFatalError).toHaveBeenCalledWith(thrownError) }) + + it('should log the thrownError', () => { + connection._handleFatalError(thrownError) + + expect(loggerFunction).toHaveBeenCalledWith( + 'error', + `${connection} experienced a fatal error caused by Neo4jError: some error ` + + '({"code":"C","name":"Neo4jError"})' + ) + }) }) describe('when there is current failure on going', () => { const thrownError = newError('some error', 'C') const currentFailure = newError('current failure', 'ongoing') + let loggerFunction; let notifyFatalError; let connection; @@ -174,9 +187,10 @@ describe('ChannelConnection', () => { notifyFatalError, currentFailure } - + loggerFunction = jest.fn() + const logger = new Logger('info', loggerFunction) const protocolSupplier = () => protocol - connection = spyOnConnectionChannel({ protocolSupplier }) + connection = spyOnConnectionChannel({ protocolSupplier, logger }) }) it('should set connection state to broken', () => { @@ -196,6 +210,16 @@ describe('ChannelConnection', () => { expect(notifyFatalError).toHaveBeenCalledWith(currentFailure) }) + + it('should log the currentFailure', () => { + connection._handleFatalError(thrownError) + + expect(loggerFunction).toHaveBeenCalledWith( + 'error', + `${connection} experienced a fatal error caused by Neo4jError: current failure ` + + '({"code":"ongoing","name":"Neo4jError"})' + ) + }) }) })