Skip to content

Commit ba45677

Browse files
fix: Fix error handling to extract error information from the response body (#738)
* Fix error handling to process response body * lint * Test for passing API error messages in GaxiosError message property
1 parent 8d9ce87 commit ba45677

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

core/packages/gaxios/src/gaxios.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ export class Gaxios implements FetchCompliance {
196196
if (opts.responseType === 'stream') {
197197
const response = [];
198198

199-
for await (const chunk of (opts.data ?? []) as Readable) {
199+
for await (const chunk of translatedResponse.data as Readable) {
200200
response.push(chunk);
201201
}
202202

203-
translatedResponse.data = response as T;
203+
translatedResponse.data = response.toString() as T;
204204
}
205205

206206
const errorInfo = GaxiosError.extractAPIErrorFromResponse(

core/packages/gaxios/test/test.getch.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,36 @@ describe('🚙 error handling', () => {
113113
);
114114
});
115115

116+
it('should handle AIP-193 error bodies: passes API error in message', async () => {
117+
const body = {
118+
error: {
119+
code: 429,
120+
message:
121+
"The zone 'us-east1-a' does not have enough resources available to fulfill the request. Try a different zone, or try again later.",
122+
status: 'RESOURCE_EXHAUSTED',
123+
details: [],
124+
},
125+
};
126+
const readableStream = Readable.from(JSON.stringify(body));
127+
const scope = nock(url).get('/').reply(429, readableStream);
128+
129+
await assert.rejects(
130+
request<ArrayBuffer>({url, responseType: 'stream'}),
131+
(err: GaxiosError) => {
132+
scope.done();
133+
const apiError = JSON.parse(err.message);
134+
return (
135+
apiError.error.code === 429 &&
136+
apiError.error.message ===
137+
"The zone 'us-east1-a' does not have enough resources available to fulfill the request. Try a different zone, or try again later." &&
138+
apiError.error.status === 'RESOURCE_EXHAUSTED' &&
139+
Array.isArray(apiError.error.details) &&
140+
apiError.error.details.length === 0
141+
);
142+
},
143+
);
144+
});
145+
116146
it('should not throw an error during a translation error', () => {
117147
const notJSON = '.';
118148
const response = {

0 commit comments

Comments
 (0)