Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion core/packages/gaxios/src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,13 @@ export class Gaxios implements FetchCompliance {
response.push(chunk);
}

translatedResponse.data = response.toString() as T;
if (response.every(chunk => typeof chunk === 'string')) {
translatedResponse.data = response.join('') as T;
} else {
translatedResponse.data = Buffer.concat(
response.map(c => (typeof c === 'string' ? Buffer.from(c) : c)),
).toString('utf8') as T;
}
}

const errorInfo = GaxiosError.extractAPIErrorFromResponse(
Expand Down
23 changes: 23 additions & 0 deletions core/packages/gaxios/test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ describe('🚙 error handling', () => {
);
});

it('should handle stream error responses split across multiple chunks without corrupting them with commas', async () => {
const chunks = [
'{"error": {"code": 400, ',
'"message": "Invalid ',
'argument", "status": "INVALID_ARGUMENT"}}',
];
const readableStream = Readable.from(chunks);
const scope = nock(url).get('/').reply(400, readableStream);

await assert.rejects(
request({url, responseType: 'stream'}),
(err: GaxiosError) => {
scope.done();
const apiError = JSON.parse(err.message);
return (
apiError.error.code === 400 &&
apiError.error.message === 'Invalid argument' &&
apiError.error.status === 'INVALID_ARGUMENT'
);
},
);
});
Comment thread
pearigee marked this conversation as resolved.
Outdated

it('should not throw an error during a translation error', () => {
const notJSON = '.';
const response = {
Expand Down
Loading