From 93c430c10556a5f2fbcc0c6373cadd431333774a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cruz?= Date: Thu, 3 Mar 2016 01:35:59 +0000 Subject: [PATCH] Add statusCode to ParseError, closes #182. --- index.js | 9 +++++++-- test/json.js | 9 +++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index a61eca229..6bca47d48 100644 --- a/index.js +++ b/index.js @@ -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); } } @@ -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); diff --git a/test/json.js b/test/json.js index d2dbabb5f..bd61c0ef5 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 t => { await s.close(); });