diff --git a/index.js b/index.js index 1a4d91d..0ee457e 100644 --- a/index.js +++ b/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)}...`; }); diff --git a/test/json.js b/test/json.js index 3266714..617bc12 100644 --- a/test/json.js +++ b/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(); });