Skip to content

Commit

Permalink
Revert "fix: translate GaxiosError message to object regardless of re…
Browse files Browse the repository at this point in the history
…turn type (#537)"

This reverts commit 563c653.
  • Loading branch information
ddelgrosso1 committed Jun 25, 2023
1 parent b64ef9a commit 4850143
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 70 deletions.
16 changes: 0 additions & 16 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export class GaxiosError<T = any> extends Error {
super(message);
this.response = response;
this.config = options;
this.response.data = translateData(options.responseType, response.data);
this.code = response.status.toString();
}
}
Expand Down Expand Up @@ -203,18 +202,3 @@ export interface FetchHeaders {
thisArg?: any
): void;
}

function translateData(responseType: string | undefined, data: any) {
switch (responseType) {
case 'stream':
return data;
case 'json':
return JSON.parse(JSON.stringify(data));
case 'arraybuffer':
return JSON.parse(Buffer.from(data).toString('utf8'));
case 'blob':
return JSON.parse(data.text());
default:
return JSON.parse(data.text());
}
}
11 changes: 0 additions & 11 deletions src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
Headers,
} from './common';
import {getRetryConfig} from './retry';
import {Stream} from 'stream';

/* eslint-disable @typescript-eslint/no-explicit-any */

Expand Down Expand Up @@ -156,16 +155,6 @@ export class Gaxios {
translatedResponse = await this._defaultAdapter(opts);
}
if (!opts.validateStatus!(translatedResponse.status)) {
if (opts.responseType === 'stream') {
let response = '';
await new Promise(resolve => {
(translatedResponse.data as Stream).on('data', chunk => {
response += chunk;
});
(translatedResponse.data as Stream).on('end', resolve);
});
translatedResponse.data = response as T;
}
throw new GaxiosError<T>(
`Request failed with status code ${translatedResponse.status}`,
opts,
Expand Down
43 changes: 0 additions & 43 deletions test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,49 +56,6 @@ describe('🚙 error handling', () => {
return err.code === '500';
});
});

it('should throw the error as a GaxiosError object, regardless of Content-Type header', async () => {
const body = {
error: {
code: 404,
message: 'File not found',
},
};
const scope = nock(url).get('/').reply(404, body);
await assert.rejects(
request<JSON>({url, responseType: 'json'}),
(err: GaxiosError) => {
scope.done();
return (
err.code === '404' &&
err.message === 'Request failed with status code 404' &&
err.response?.data.error.message === 'File not found'
);
}
);
});

it('should throw the error as a GaxiosError object (with the message as a string), even if the request type is requested as an arraybuffer', async () => {
const body = {
error: {
code: 404,
message: 'File not found',
},
};
const scope = nock(url).get('/').reply(404, body);

await assert.rejects(
request<ArrayBuffer>({url, responseType: 'arraybuffer'}),
(err: GaxiosError) => {
scope.done();
return (
err.code === '404' &&
err.message === 'Request failed with status code 404' &&
err.response?.data.error.message === 'File not found'
);
}
);
});
});

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

0 comments on commit 4850143

Please sign in to comment.