From bcfd9901b3d8b33cda314e3d185fb01a0d1743b0 Mon Sep 17 00:00:00 2001 From: mmaruniak Date: Thu, 8 Feb 2024 13:44:59 +0100 Subject: [PATCH] Mm/prc 0 propagate error details (#57) Release 5.3.2 --- CHANGELOG.md | 6 ++++++ package.json | 2 +- src/util.ts | 2 +- tests/util.test.ts | 30 ++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2de5084..f3faa97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +## [5.3.2] - 2024-02-08 + +### Fixed + +Error details of external HTTP error resposes are propagated correctly + ## [5.3.1] - 2023-10-25 ### Fixed diff --git a/package.json b/package.json index df96ef4..630c810 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lambda-essentials-ts", - "version": "5.3.1", + "version": "5.3.2", "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/util.ts b/src/util.ts index af958da..0e4dc68 100644 --- a/src/util.ts +++ b/src/util.ts @@ -50,7 +50,7 @@ export function serializeAxiosError(error: AxiosError): SerializedAxiosError | u const { status, data } = error.response; return { status: data.originalStatusCode ?? status, // Propagate original status code of ClientException - details: data.details ? data.details : data, // Prevent wrapping of Exception + details: data.details && Object.keys(data.details).length > 0 ? data.details : data, // Prevent wrapping of Exception }; } diff --git a/tests/util.test.ts b/tests/util.test.ts index 5a78004..2a60f1b 100644 --- a/tests/util.test.ts +++ b/tests/util.test.ts @@ -115,6 +115,36 @@ describe('Util', () => { const serializedError = serializeAxiosError(error as any); expect(serializedError).toEqual(expected); }); + + test('serializes non-Exception objects which also have "details" field', () => { + const expected = { + details: { + details: {}, + error, + }, + status: 500, + }; + + const axiosErrorWithNonExceptionError: AxiosError = { + isAxiosError: true, + message: 'test-message', + response: { + data: { + details: {}, + error, + }, + status: 500, + config: {}, + headers: null, + statusText: 'test-status-text', + }, + config: {}, + name: 'test-name', + toJSON: () => ({}), + }; + const serializedError = serializeAxiosError(axiosErrorWithNonExceptionError as any); + expect(serializedError).toEqual(expected); + }); }); describe('redactSecret', () => {