Browse Source

Add statusCode to ParseError, closes #182.

node-7
André Cruz 9 years ago
parent
commit
26a499c033
  1. 6
      index.js
  2. 9
      test/json.js

6
index.js

@ -105,7 +105,7 @@ function asPromise(opts) {
try {
res.body = JSON.parse(res.body);
} catch (e) {
throw new got.ParseError(e, opts, data);
throw new got.ParseError(e, statusCode, opts, data);
}
}
@ -318,8 +318,10 @@ function stdError(error, opts) {
got.RequestError = createErrorClass('RequestError', stdError);
got.ReadError = createErrorClass('ReadError', stdError);
got.ParseError = createErrorClass('ParseError', function (e, opts, data) {
got.ParseError = createErrorClass('ParseError', function (e, statusCode, opts, data) {
stdError.call(this, e, opts);
this.statusCode = statusCode;
this.statusMessage = nodeStatusCodes[this.statusCode];
this.message = `${e.message} in "${urlLib.format(opts)}": \n${data.slice(0, 77)}...`;
});

9
test/json.js

@ -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 () => {
await s.close();
});

Loading…
Cancel
Save