Skip to content

Commit

Permalink
fix: don't throw an error within a GaxiosError
Browse files Browse the repository at this point in the history
  • Loading branch information
d-goog committed Sep 7, 2023
1 parent fdb6a8e commit 9f3e121
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ export class GaxiosError<T = any> extends Error {
super(message);

if (this.response) {
this.response.data = translateData(config.responseType, response?.data);
try {
this.response.data = translateData(config.responseType, response?.data);
} catch {
// best effort - don't throw an error within an error
// we could set `this.response.config.responseType = 'unknown'`, but
// that would mutate future calls with this config object.
}

this.status = this.response.status;
}

Expand Down Expand Up @@ -357,7 +364,7 @@ export function defaultErrorRedactor<T = any>(data: {

data.config.url = url.toString();
} catch {
// ignore error
// ignore error - no need to parse an invalid URL
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ describe('🚙 error handling', () => {
}
);
});

it('should not throw an error during a translation error', () => {
const notJSON = '.';
const response: GaxiosResponse = {
config: {
responseType: 'json',
},
data: notJSON,
status: 500,
statusText: '',
headers: {},
request: {
responseURL: url,
},
};

const error = new GaxiosError('translation test', {}, response);

assert(error.response, undefined);
assert.equal(error.response.data, notJSON);
});
});

describe('🥁 configuration options', () => {
Expand Down

0 comments on commit 9f3e121

Please sign in to comment.