Skip to content

Commit

Permalink
Include invalid JSON body in ParseErrors (#1044)
Browse files Browse the repository at this point in the history
  • Loading branch information
hermo authored and szmarczak committed Jan 28, 2020
1 parent 7f71571 commit 2813b73
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
6 changes: 3 additions & 3 deletions source/as-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ export default function asPromise<T>(options: NormalizedOptions): CancelableRequ
try {
response.body = parseBody(body, options.responseType, options.encoding);
} catch (error) {
// Fall back to `utf8`
response.body = body.toString();

if (isOk()) {
const parseError = new ParseError(error, response, options);
emitError(parseError);
return;
}

// Fall back to `utf8`
response.body = body.toString();
}

try {
Expand Down
1 change: 1 addition & 0 deletions test/response-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ test('parse errors have `response` property', withServer, async (t, server, got)
const error = await t.throwsAsync<ParseError>(got({responseType: 'json'}), {instanceOf: ParseError});

t.is(error.response.statusCode, 200);
t.is(error.response.body, '/');
});

test('sets correct headers', withServer, async (t, server, got) => {
Expand Down

0 comments on commit 2813b73

Please sign in to comment.