Skip to content

Commit 01c2636

Browse files
sleewoosindresorhus
authored andcommitted
Use statusMessage returned by endpoint (#398)
1 parent 73b5614 commit 01c2636

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ function asPromise(opts) {
324324
}
325325

326326
if (statusCode !== 304 && (statusCode < 200 || statusCode > limitStatusCode)) {
327-
throw new got.HTTPError(statusCode, res.headers, opts);
327+
throw new got.HTTPError(statusCode, res.statusMessage, res.headers, opts);
328328
}
329329

330330
resolve(res);
@@ -407,7 +407,7 @@ function asStream(opts) {
407407
res.pipe(output);
408408

409409
if (statusCode !== 304 && (statusCode < 200 || statusCode > 299)) {
410-
proxy.emit('error', new got.HTTPError(statusCode, res.headers, opts), null, res);
410+
proxy.emit('error', new got.HTTPError(statusCode, res.statusMessage, res.headers, opts), null, res);
411411
return;
412412
}
413413

@@ -627,8 +627,12 @@ got.ParseError = class extends StdError {
627627
};
628628

629629
got.HTTPError = class extends StdError {
630-
constructor(statusCode, headers, opts) {
631-
const statusMessage = http.STATUS_CODES[statusCode];
630+
constructor(statusCode, statusMessage, headers, opts) {
631+
if (statusMessage) {
632+
statusMessage = statusMessage.replace(/\r?\n/g, ' ').trim();
633+
} else {
634+
statusMessage = http.STATUS_CODES[statusCode];
635+
}
632636
super(`Response code ${statusCode} (${statusMessage})`, {}, opts);
633637
this.name = 'HTTPError';
634638
this.statusCode = statusCode;

test/error.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ test.before('setup', async () => {
1414
res.end('not');
1515
});
1616

17+
s.on('/default-status-message', (req, res) => {
18+
res.statusCode = 400;
19+
res.end('body');
20+
});
21+
22+
s.on('/custom-status-message', (req, res) => {
23+
res.statusCode = 400;
24+
res.statusMessage = 'Something Exploded';
25+
res.end('body');
26+
});
27+
1728
await s.listen(s.port);
1829
});
1930

@@ -45,6 +56,18 @@ test('options.body error message', async t => {
4556
t.regex(err.message, /options\.body must be a ReadableStream, string, Buffer or plain Object/);
4657
});
4758

59+
test('default status message', async t => {
60+
const err = await t.throws(got(`${s.url}/default-status-message`));
61+
t.is(err.statusCode, 400);
62+
t.is(err.statusMessage, 'Bad Request');
63+
});
64+
65+
test('custom status message', async t => {
66+
const err = await t.throws(got(`${s.url}/custom-status-message`));
67+
t.is(err.statusCode, 400);
68+
t.is(err.statusMessage, 'Something Exploded');
69+
});
70+
4871
test.serial('http.request error', async t => {
4972
const stub = sinon.stub(http, 'request').callsFake(() => {
5073
throw new TypeError('The header content contains invalid characters');

0 commit comments

Comments
 (0)