Skip to content

Commit

Permalink
Add statusCode to ParseError, closes #182.
Browse files Browse the repository at this point in the history
  • Loading branch information
satazor authored and floatdrop committed Mar 3, 2016
1 parent ba09d36 commit 93c430c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function asCallback(opts, cb) {
data = parseJson(data);
} catch (e) {
e.fileName = urlLib.format(opts);
err = new got.ParseError(e, opts);
err = new got.ParseError(e, statusCode, opts);
}
}

Expand Down Expand Up @@ -362,7 +362,12 @@ function stdError(error, opts) {

got.RequestError = createErrorClass('RequestError', stdError);
got.ReadError = createErrorClass('ReadError', stdError);
got.ParseError = createErrorClass('ParseError', stdError);

got.ParseError = createErrorClass('ParseError', function (e, statusCode, opts) {
stdError.call(this, e, opts);
this.statusCode = statusCode;
this.statusMessage = nodeStatusCodes[this.statusCode];
});

got.HTTPError = createErrorClass('HTTPError', function (statusCode, opts) {
stdError.call(this, {}, opts);
Expand Down
9 changes: 9 additions & 0 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ test('catches errors on invalid non-200 responses', async t => {
}
});

test('should have statusCode in err', async t => {
try {
await got(`${s.url}/non200-invalid`, {json: true});
t.fail('Exception was not thrown');
} catch (err) {
t.is(err.statusCode, 500);
}
});

test.after('cleanup', async t => {
await s.close();
});

0 comments on commit 93c430c

Please sign in to comment.