diff --git a/package.json b/package.json index ae8e379..f49c560 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lambda-essentials-ts", - "version": "6.0.0", + "version": "6.1.0", "description": "A selection of the finest modules supporting authorization, API routing, error handling, logging and sending HTTP requests.", "main": "lib/index.js", "private": false, diff --git a/src/exceptions/clientException.ts b/src/exceptions/clientException.ts index 37f4ab1..4422ac0 100644 --- a/src/exceptions/clientException.ts +++ b/src/exceptions/clientException.ts @@ -17,10 +17,11 @@ export class ClientException extends Exception { serviceName: string, originalStatusCode?: number, details?: any, + originalMessage?: string, statusCodeMapOverride?: Record, ) { super( - `Dependent service "${serviceName}" returned error`, + `Dependent service "${serviceName}" returned error: ${originalMessage ?? 'Unknown error'}`, ClientException.convertStatusCode(originalStatusCode, statusCodeMapOverride), details, ); diff --git a/src/httpClient/httpClient.ts b/src/httpClient/httpClient.ts index dbf0538..ef49c06 100644 --- a/src/httpClient/httpClient.ts +++ b/src/httpClient/httpClient.ts @@ -138,6 +138,7 @@ export default class HttpClient { hostname, serializedAxiosError?.status, serializedAxiosError?.details, + serializedAxiosError?.message, this.clientExceptionStatusCodeMapOverride, ); }, @@ -187,6 +188,7 @@ export default class HttpClient { hostname, serializedAxiosError?.status, serializedAxiosError?.details, + serializedAxiosError?.message, this.clientExceptionStatusCodeMapOverride, ); }, diff --git a/src/util.ts b/src/util.ts index 0e4dc68..0913783 100644 --- a/src/util.ts +++ b/src/util.ts @@ -51,10 +51,12 @@ export function serializeAxiosError(error: AxiosError): SerializedAxiosError | u return { status: data.originalStatusCode ?? status, // Propagate original status code of ClientException details: data.details && Object.keys(data.details).length > 0 ? data.details : data, // Prevent wrapping of Exception + message: data.message, }; } export interface SerializedAxiosError { status: number; details: any; + message?: string; } diff --git a/tests/exceptions/clientException.test.ts b/tests/exceptions/clientException.test.ts index eebae98..c23e75b 100644 --- a/tests/exceptions/clientException.test.ts +++ b/tests/exceptions/clientException.test.ts @@ -44,7 +44,7 @@ describe('ClientException', () => { const clientException = new ClientException(testServiceName, axiosError.status, axiosError); expect(clientException.message).toEqual( - `Dependent service "${testServiceName}" returned error`, + `Dependent service "${testServiceName}" returned error: Unknown error`, ); expect(clientException.originalStatusCode).toEqual(originalStatusCode); expect(clientException.statusCode).toEqual(exceptionStatusCode); @@ -61,6 +61,7 @@ describe('ClientException', () => { const axiosError: SerializedAxiosError = { status: originalStatusCode, details: [], + message: 'test-message', }; const clientExceptionStatusCodeMapOverride = { 403: 503, @@ -70,11 +71,12 @@ describe('ClientException', () => { testServiceName, axiosError.status, axiosError, + axiosError.message, clientExceptionStatusCodeMapOverride, ); expect(clientException.message).toEqual( - `Dependent service "${testServiceName}" returned error`, + `Dependent service "${testServiceName}" returned error: test-message`, ); expect(clientException.originalStatusCode).toEqual(originalStatusCode); expect(clientException.statusCode).toEqual(expectedStatusCode); diff --git a/tests/openApi/openApi.test.ts b/tests/openApi/openApi.test.ts index 5a3c00a..9adae93 100644 --- a/tests/openApi/openApi.test.ts +++ b/tests/openApi/openApi.test.ts @@ -220,11 +220,16 @@ describe('Open API Wrapper', () => { test('returns 500 and logs INFO with details when ClientException exception is caught', async () => { const testServiceName = 'test-service-name'; - const exception = new ClientException(testServiceName, 409, { more: 'details' }); + const exception = new ClientException( + testServiceName, + 409, + { more: 'details' }, + 'test-message', + ); const expected: Partial = { body: { details: exception.details, - title: `Dependent service "${testServiceName}" returned error`, + title: `Dependent service "${testServiceName}" returned error: test-message`, }, statusCode: 503, headers: { @@ -245,7 +250,7 @@ describe('Open API Wrapper', () => { title: 'ErrorLogger', details: exception.details, statusCode: 503, - message: `Dependent service "${testServiceName}" returned error`, + message: `Dependent service "${testServiceName}" returned error: test-message`, originalStatusCode: 409, serviceName: testServiceName, stack: expect.any(String),