Skip to content

Commit 4228412

Browse files
authored
fix: JSON stringify Axios error response data (#5585) (#5588)
1 parent 3a77fd9 commit 4228412

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

.changeset/clever-grapes-dance.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sap-cloud-sdk/util': patch
3+
---
4+
5+
[Fixed Issue] Stringify Axios response data object in the error stack of `ErrorWithCause` class.

.github/actions/check-public-api/index.js

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/util/src/error-with-cause.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ErrorWithCause } from './error-with-cause';
2+
import type { AxiosError } from 'axios';
23

34
describe('ErrorWithCause', () => {
45
it('creates an error', () => {
@@ -32,4 +33,32 @@ describe('ErrorWithCause', () => {
3233
}"
3334
`);
3435
});
36+
37+
it('adds Axios error to stack', () => {
38+
const axiosError: AxiosError = {
39+
message: 'Request failed with status code 400',
40+
name: 'AxiosError',
41+
stack: 'AxiosError: Request failed with status code 400',
42+
code: 'ERR_BAD_REQUEST',
43+
status: 400,
44+
response: {
45+
data: {
46+
my_error: {
47+
my_code: 'Four Hundred',
48+
my_message: 'This is a bad request'
49+
}
50+
}
51+
} as any,
52+
isAxiosError: true,
53+
toJSON: () => ({})
54+
};
55+
const err = new ErrorWithCause('message', axiosError);
56+
expect(err.stack).toContain('ErrorWithCause: message');
57+
expect(err.stack).toContain('at ');
58+
expect(err.stack).toContain('Caused by:');
59+
expect(err.stack).toContain(
60+
'HTTP Response: Request failed with status code 400'
61+
);
62+
expect(err.stack).toContain('Four Hundred');
63+
});
3564
});

packages/util/src/error-with-cause.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { unixEOL } from './string-formatter';
2+
import { createLogger } from './logger';
23
import type { AxiosError } from 'axios';
34

5+
const logger = createLogger({
6+
package: 'util',
7+
messageContext: 'error-with-cause'
8+
});
9+
410
/**
511
* Represents an error that was caused by another error.
612
*/
@@ -29,7 +35,15 @@ export class ErrorWithCause extends Error {
2935
private addStack(cause: Error) {
3036
// Axios removed the stack property in version 0.27 which gave no useful information anyway. This adds the http cause.
3137
if (this.isAxiosError(cause)) {
32-
const response = cause.response?.data ? ` - ${cause.response?.data}` : '';
38+
let response = '';
39+
if (cause.response?.data) {
40+
try {
41+
response = `${unixEOL}${JSON.stringify(cause.response?.data, null, 2)}`;
42+
} catch (error) {
43+
logger.warn(`Failed to stringify response data: ${error.message}`);
44+
response = `${unixEOL}${cause.response?.data}`;
45+
}
46+
}
3347
this.stack = `${this.stack}${unixEOL}Caused by:${unixEOL}HTTP Response: ${cause.message}${response}`;
3448
} else if (this.stack && cause?.stack) {
3549
// Stack is a non-standard property according to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Custom_Error_Types

0 commit comments

Comments
 (0)