Browse Source

Use statusMessage returned by endpoint (#398)

master
Slee Woo 7 years ago
committed by Sindre Sorhus
parent
commit
01c26364d9
  1. 12
      index.js
  2. 23
      test/error.js

12
index.js

@ -324,7 +324,7 @@ function asPromise(opts) {
} }
if (statusCode !== 304 && (statusCode < 200 || statusCode > limitStatusCode)) { if (statusCode !== 304 && (statusCode < 200 || statusCode > limitStatusCode)) {
throw new got.HTTPError(statusCode, res.headers, opts); throw new got.HTTPError(statusCode, res.statusMessage, res.headers, opts);
} }
resolve(res); resolve(res);
@ -407,7 +407,7 @@ function asStream(opts) {
res.pipe(output); res.pipe(output);
if (statusCode !== 304 && (statusCode < 200 || statusCode > 299)) { if (statusCode !== 304 && (statusCode < 200 || statusCode > 299)) {
proxy.emit('error', new got.HTTPError(statusCode, res.headers, opts), null, res); proxy.emit('error', new got.HTTPError(statusCode, res.statusMessage, res.headers, opts), null, res);
return; return;
} }
@ -627,8 +627,12 @@ got.ParseError = class extends StdError {
}; };
got.HTTPError = class extends StdError { got.HTTPError = class extends StdError {
constructor(statusCode, headers, opts) { constructor(statusCode, statusMessage, headers, opts) {
const statusMessage = http.STATUS_CODES[statusCode]; if (statusMessage) {
statusMessage = statusMessage.replace(/\r?\n/g, ' ').trim();
} else {
statusMessage = http.STATUS_CODES[statusCode];
}
super(`Response code ${statusCode} (${statusMessage})`, {}, opts); super(`Response code ${statusCode} (${statusMessage})`, {}, opts);
this.name = 'HTTPError'; this.name = 'HTTPError';
this.statusCode = statusCode; this.statusCode = statusCode;

23
test/error.js

@ -14,6 +14,17 @@ test.before('setup', async () => {
res.end('not'); res.end('not');
}); });
s.on('/default-status-message', (req, res) => {
res.statusCode = 400;
res.end('body');
});
s.on('/custom-status-message', (req, res) => {
res.statusCode = 400;
res.statusMessage = 'Something Exploded';
res.end('body');
});
await s.listen(s.port); await s.listen(s.port);
}); });
@ -45,6 +56,18 @@ test('options.body error message', async t => {
t.regex(err.message, /options\.body must be a ReadableStream, string, Buffer or plain Object/); t.regex(err.message, /options\.body must be a ReadableStream, string, Buffer or plain Object/);
}); });
test('default status message', async t => {
const err = await t.throws(got(`${s.url}/default-status-message`));
t.is(err.statusCode, 400);
t.is(err.statusMessage, 'Bad Request');
});
test('custom status message', async t => {
const err = await t.throws(got(`${s.url}/custom-status-message`));
t.is(err.statusCode, 400);
t.is(err.statusMessage, 'Something Exploded');
});
test.serial('http.request error', async t => { test.serial('http.request error', async t => {
const stub = sinon.stub(http, 'request').callsFake(() => { const stub = sinon.stub(http, 'request').callsFake(() => {
throw new TypeError('The header content contains invalid characters'); throw new TypeError('The header content contains invalid characters');

Loading…
Cancel
Save