Skip to content

Commit

Permalink
fix: circular json bugsnag (#3713)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanpj2292 authored Sep 6, 2024
1 parent f7783d8 commit 263d075
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/v0/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,18 @@ const getErrorStatusCode = (error, defaultStatusCode = HTTP_STATUS_CODES.INTERNA
}
};

function isAxiosError(err) {
return (
Array.isArray(err?.config?.adapter) &&
err?.config?.adapter?.length > 1 &&
typeof err?.request?.socket === 'object' &&
!!err?.request?.protocol &&
!!err?.request?.method &&
!!err?.request?.path &&
!!err?.status
);
}

/**
* Used for generating error response with stats from native and built errors
*/
Expand All @@ -1567,11 +1579,15 @@ function generateErrorObject(error, defTags = {}, shouldEnrichErrorMessage = tru
error.authErrorCategory,
);
let errorMessage = error.message;
if (isAxiosError(errObject.destinationResponse)) {
delete errObject?.destinationResponse.config;
delete errObject?.destinationResponse.request;
}
if (shouldEnrichErrorMessage) {
if (error.destinationResponse) {
if (errObject.destinationResponse) {
errorMessage = JSON.stringify({
message: errorMessage,
destinationResponse: error.destinationResponse,
destinationResponse: errObject.destinationResponse,
});
}
errObject.message = errorMessage;
Expand Down Expand Up @@ -2411,4 +2427,5 @@ module.exports = {
validateEventAndLowerCaseConversion,
getRelativePathFromURL,
removeEmptyKey,
isAxiosError,
};
124 changes: 124 additions & 0 deletions src/v0/util/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
generateExclusionList,
combineBatchRequestsWithSameJobIds,
validateEventAndLowerCaseConversion,
isAxiosError,
} = require('./index');
const exp = require('constants');

Expand Down Expand Up @@ -735,3 +736,126 @@ describe('get relative path from url', () => {
expect(utilities.getRelativePathFromURL(null)).toEqual(null);
});
});

describe('isAxiosError', () => {
const validAxiosError = {
config: {
adapter: ['xhr', 'fetch'],
},
request: {
socket: {},
protocol: 'https:',
headers: {},
method: 'GET',
path: '/api/data',
},
status: 404,
statusText: 'Not Found',
};

it('should return true for a valid Axios error object', () => {
expect(isAxiosError(validAxiosError)).toBe(true);
});

it('should return false for null', () => {
expect(isAxiosError(null)).toBe(false);
});

it('should return false for undefined', () => {
expect(isAxiosError(undefined)).toBe(false);
});

it('should return false for non-object types', () => {
expect(isAxiosError('string')).toBe(false);
expect(isAxiosError(123)).toBe(false);
expect(isAxiosError(true)).toBe(false);
expect(isAxiosError([])).toBe(false);
});

it('should return false for an empty object', () => {
expect(isAxiosError({})).toBe(false);
});

it('should return false when config is missing', () => {
const { config, ...errorWithoutConfig } = validAxiosError;
expect(isAxiosError(errorWithoutConfig)).toBe(false);
});

it('should return false when config.adapter is not an array', () => {
const error = { ...validAxiosError, config: { adapter: 'not an array' } };
expect(isAxiosError(error)).toBe(false);
});

it('should return false when config.adapter has length <= 1', () => {
const error = { ...validAxiosError, config: { adapter: ['some'] } };
expect(isAxiosError(error)).toBe(false);
});

it('should return false when request is missing', () => {
const { request, ...errorWithoutRequest } = validAxiosError;
expect(isAxiosError(errorWithoutRequest)).toBe(false);
});

it('should return false when request.socket is missing', () => {
const error = {
...validAxiosError,
request: { ...validAxiosError.request, socket: undefined },
};
expect(isAxiosError(error)).toBe(false);
});

it('should return false when request.socket is not an object', () => {
const error = {
...validAxiosError,
request: { ...validAxiosError.request, socket: 'not an object' },
};
expect(isAxiosError(error)).toBe(false);
});

it('should return false when request.protocol is missing', () => {
const error = {
...validAxiosError,
request: { ...validAxiosError.request, protocol: undefined },
};
expect(isAxiosError(error)).toBe(false);
});

it('should return false when request.method is missing', () => {
const error = {
...validAxiosError,
request: { ...validAxiosError.request, method: undefined },
};
expect(isAxiosError(error)).toBe(false);
});

it('should return false when request.path is missing', () => {
const error = {
...validAxiosError,
request: { ...validAxiosError.request, path: undefined },
};
expect(isAxiosError(error)).toBe(false);
});

it('should return false when status is missing', () => {
const { status, ...errorWithoutStatus } = validAxiosError;
expect(isAxiosError(errorWithoutStatus)).toBe(false);
});

it('should return true when all required properties are present and valid, even with extra properties', () => {
const errorWithExtraProps = {
...validAxiosError,
extraProp: 'some value',
};
expect(isAxiosError(errorWithExtraProps)).toBe(true);
});

it('should return false when config.adapter is an empty array', () => {
const error = { ...validAxiosError, config: { adapter: [] } };
expect(isAxiosError(error)).toBe(false);
});

it('should return false when status is 0', () => {
const error = { ...validAxiosError, status: 0 };
expect(isAxiosError(error)).toBe(false);
});
});

0 comments on commit 263d075

Please sign in to comment.