Skip to content

Commit

Permalink
Preserve the original message in ClientException (#61)
Browse files Browse the repository at this point in the history
Release 6.1.0
  • Loading branch information
mmaruniak authored Sep 30, 2024
1 parent d35fbf4 commit 05f062d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/exceptions/clientException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ export class ClientException extends Exception {
serviceName: string,
originalStatusCode?: number,
details?: any,
originalMessage?: string,
statusCodeMapOverride?: Record<number, number>,
) {
super(
`Dependent service "${serviceName}" returned error`,
`Dependent service "${serviceName}" returned error: ${originalMessage ?? 'Unknown error'}`,
ClientException.convertStatusCode(originalStatusCode, statusCodeMapOverride),
details,
);
Expand Down
2 changes: 2 additions & 0 deletions src/httpClient/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export default class HttpClient {
hostname,
serializedAxiosError?.status,
serializedAxiosError?.details,
serializedAxiosError?.message,
this.clientExceptionStatusCodeMapOverride,
);
},
Expand Down Expand Up @@ -187,6 +188,7 @@ export default class HttpClient {
hostname,
serializedAxiosError?.status,
serializedAxiosError?.details,
serializedAxiosError?.message,
this.clientExceptionStatusCodeMapOverride,
);
},
Expand Down
2 changes: 2 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 4 additions & 2 deletions tests/exceptions/clientException.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -61,6 +61,7 @@ describe('ClientException', () => {
const axiosError: SerializedAxiosError = {
status: originalStatusCode,
details: [],
message: 'test-message',
};
const clientExceptionStatusCodeMapOverride = {
403: 503,
Expand All @@ -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);
Expand Down
11 changes: 8 additions & 3 deletions tests/openApi/openApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApiResponse> = {
body: {
details: exception.details,
title: `Dependent service "${testServiceName}" returned error`,
title: `Dependent service "${testServiceName}" returned error: test-message`,
},
statusCode: 503,
headers: {
Expand All @@ -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),
Expand Down

0 comments on commit 05f062d

Please sign in to comment.