From b09e6b915fe86b6b6bcb119eb8338b7a6863c30a Mon Sep 17 00:00:00 2001 From: Denis DelGrosso <85250797+ddelgrosso1@users.noreply.github.com> Date: Sun, 25 Jun 2023 11:17:50 -0400 Subject: [PATCH] Revert "fix: translate GaxiosError message to object regardless of return type (#537)" (#543) This reverts commit 563c6537a06bc64d5c6e918090c00ec7a586cecb. --- src/common.ts | 16 ---------------- src/gaxios.ts | 11 ----------- test/test.getch.ts | 43 ------------------------------------------- 3 files changed, 70 deletions(-) diff --git a/src/common.ts b/src/common.ts index 5160ecef..4513cdc3 100644 --- a/src/common.ts +++ b/src/common.ts @@ -28,7 +28,6 @@ export class GaxiosError extends Error { super(message); this.response = response; this.config = options; - this.response.data = translateData(options.responseType, response.data); this.code = response.status.toString(); } } @@ -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()); - } -} diff --git a/src/gaxios.ts b/src/gaxios.ts index fc535ca6..7267af30 100644 --- a/src/gaxios.ts +++ b/src/gaxios.ts @@ -28,7 +28,6 @@ import { Headers, } from './common'; import {getRetryConfig} from './retry'; -import {Stream} from 'stream'; /* eslint-disable @typescript-eslint/no-explicit-any */ @@ -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( `Request failed with status code ${translatedResponse.status}`, opts, diff --git a/test/test.getch.ts b/test/test.getch.ts index c3397db9..0297a188 100644 --- a/test/test.getch.ts +++ b/test/test.getch.ts @@ -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({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({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', () => {