Skip to content

Commit 1714769

Browse files
Tyler-Murphystephenplusplus
authored andcommitted
Includes the response in the error message when the response bod… (#449)
* Includes the response in the error message when the response body can't be parsed as JSON. * Adds ApiError to the possible types for the `err` property of `BodyResponseCallback`, since `handleResp` sometimes makes and attaches `ApiError`s. * Attaches the HTTP response to the errors when possible so that it's possible to see the raw response body.
1 parent d34c50a commit 1714769

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

core/common/src/util.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ export class PartialFailureError extends Error {
300300
}
301301

302302
export interface BodyResponseCallback {
303-
(err: Error | null, body?: ResponseBody, res?: r.Response): void;
303+
(err: Error | ApiError | null, body?: ResponseBody, res?: r.Response): void;
304304
}
305305

306306
export interface MakeRequestConfig {
@@ -367,6 +367,10 @@ export class Util {
367367
parsedResp.resp.body = parsedResp.body;
368368
}
369369

370+
if (parsedResp.err && resp) {
371+
parsedResp.err.response = resp;
372+
}
373+
370374
callback(parsedResp.err, parsedResp.body, parsedResp.resp);
371375
}
372376

@@ -404,7 +408,7 @@ export class Util {
404408
* @param {?error} parsedHttpRespMessage.err - An error detected.
405409
* @param {object} parsedHttpRespMessage.body - The original body value provided
406410
* will try to be JSON.parse'd. If it's successful, the parsed value will
407-
* be returned here, otherwise the original value.
411+
* be returned here, otherwise the original value and an error will be returned.
408412
*/
409413
parseHttpRespBody(body: ResponseBody) {
410414
const parsedHttpRespBody: ParsedHttpResponseBody = {
@@ -415,7 +419,9 @@ export class Util {
415419
try {
416420
parsedHttpRespBody.body = JSON.parse(body);
417421
} catch (err) {
418-
parsedHttpRespBody.err = new ApiError('Cannot parse JSON response');
422+
parsedHttpRespBody.err = new ApiError(
423+
`Cannot parse response as JSON: ${body}`
424+
);
419425
}
420426
}
421427

core/common/test/util.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,45 @@ describe('common/util', () => {
377377
stub('parseHttpRespBody', () => done()); // Will throw.
378378
util.handleResp(null, null, null, done);
379379
});
380+
381+
it('should surface the response body in the error message when it cannot be JSON-parsed', done => {
382+
const unparseableBody = '<html>There was some error</html>';
383+
384+
util.handleResp(null, null, unparseableBody, err => {
385+
if (err === null) {
386+
assert.fail('there should be an error');
387+
} else {
388+
assert.ok(err.message.includes(unparseableBody));
389+
}
390+
391+
done();
392+
});
393+
});
394+
395+
it('should surface the response body as an error property when it cannot be JSON-parsed', done => {
396+
const unparseableBody = '<html>There was some error</html>';
397+
398+
util.handleResp(
399+
null,
400+
{body: unparseableBody} as r.Response,
401+
unparseableBody,
402+
err => {
403+
if (err === null) {
404+
assert.fail('there should be an error');
405+
} else {
406+
const response = (err! as ApiError).response;
407+
408+
if (!response) {
409+
assert.fail('there should be a response property on the error');
410+
} else {
411+
assert.strictEqual(response.body, unparseableBody);
412+
}
413+
}
414+
415+
done();
416+
}
417+
);
418+
});
380419
});
381420

382421
describe('parseHttpRespMessage', () => {

0 commit comments

Comments
 (0)