Browse Source

Only throw ParseError on 2xx responses (#301)

remove-strictssl-option
Jakob Krigovsky 8 years ago
committed by Sindre Sorhus
parent
commit
c86706bd23
  1. 2
      index.js
  2. 2
      readme.md
  3. 8
      test/json-parse.js

2
index.js

@ -128,9 +128,11 @@ function asPromise(opts) {
try {
res.body = JSON.parse(res.body);
} catch (e) {
if (statusCode >= 200 && statusCode < 300) {
throw new got.ParseError(e, statusCode, opts, data);
}
}
}
if (statusCode !== 304 && (statusCode < 200 || statusCode > limitStatusCode)) {
throw new got.HTTPError(statusCode, res.headers, opts);

2
readme.md

@ -203,7 +203,7 @@ When reading from response stream fails.
#### got.ParseError
When `json` option is enabled and `JSON.parse` fails.
When `json` option is enabled, server response code is 2xx, and `JSON.parse` fails.
#### got.HTTPError

8
test/json-parse.js

@ -66,21 +66,21 @@ test('parses non-200 responses', async t => {
}
});
test('catches errors on invalid non-200 responses', async t => {
test('ignores errors on invalid non-200 responses', async t => {
try {
await got(`${s.url}/non200-invalid`, {json: true});
t.fail('Exception was not thrown');
} catch (err) {
t.regex(err.message, /Unexpected token/);
t.is(err.message, 'Response code 500 (Internal Server Error)');
t.is(err.response.body, 'Internal error');
t.is(err.path, '/non200-invalid');
}
});
test('should have statusCode in err', async t => {
const err = await t.throws(got(`${s.url}/non200-invalid`, {json: true}));
const err = await t.throws(got(`${s.url}/invalid`, {json: true}));
t.is(err.constructor, got.ParseError);
t.is(err.statusCode, 500);
t.is(err.statusCode, 200);
});
test('should set correct headers', async t => {

Loading…
Cancel
Save