Skip to content

Commit a9f798e

Browse files
bidipynejasnell
authored andcommitted
errors,http_server: migrate to use internal/errors.js
PR-URL: #13301 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2822796 commit a9f798e

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

lib/_http_server.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const chunkExpression = common.chunkExpression;
3535
const httpSocketSetup = common.httpSocketSetup;
3636
const OutgoingMessage = require('_http_outgoing').OutgoingMessage;
3737
const { outHeadersKey, ondrain } = require('internal/http');
38+
const errors = require('internal/errors');
3839

3940
const STATUS_CODES = {
4041
100: 'Continue',
@@ -185,7 +186,9 @@ function writeHead(statusCode, reason, obj) {
185186

186187
statusCode |= 0;
187188
if (statusCode < 100 || statusCode > 999)
188-
throw new RangeError(`Invalid status code: ${originalStatusCode}`);
189+
throw new errors.RangeError('ERR_HTTP_INVALID_STATUS_CODE',
190+
originalStatusCode);
191+
189192

190193
if (typeof reason === 'string') {
191194
// writeHead(statusCode, reasonPhrase[, headers])
@@ -211,8 +214,7 @@ function writeHead(statusCode, reason, obj) {
211214
}
212215
if (k === undefined) {
213216
if (this._header) {
214-
throw new Error('Can\'t render headers after they are sent to the ' +
215-
'client');
217+
throw new errors.Error('ERR_HTTP_HEADERS_SENT');
216218
}
217219
}
218220
// only progressive api is used
@@ -223,7 +225,8 @@ function writeHead(statusCode, reason, obj) {
223225
}
224226

225227
if (common._checkInvalidHeaderChar(this.statusMessage))
226-
throw new Error('Invalid character in statusMessage.');
228+
throw new errors.Error('ERR_HTTP_INVALID_CHAR',
229+
'Invalid character in statusMessage.');
227230

228231
var statusLine = 'HTTP/1.1 ' + statusCode + ' ' + this.statusMessage + CRLF;
229232

lib/internal/errors.js

+5
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ E('ERR_ARG_NOT_ITERABLE', '%s must be iterable');
114114
E('ERR_ASSERTION', (msg) => msg);
115115
E('ERR_CONSOLE_WRITABLE_STREAM',
116116
(name) => `Console expects a writable stream instance for ${name}`);
117+
E('ERR_HTTP_HEADERS_SENT',
118+
'Cannot render headers after they are sent to the client');
119+
E('ERR_HTTP_INVALID_CHAR', 'Invalid character in statusMessage.');
120+
E('ERR_HTTP_INVALID_STATUS_CODE',
121+
(originalStatusCode) => `Invalid status code: ${originalStatusCode}`);
117122
E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
118123
E('ERR_INVALID_ARG_TYPE', invalidArgType);
119124
E('ERR_INVALID_CALLBACK', 'callback must be a function');

test/parallel/test-http-response-statuscode.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ const MAX_REQUESTS = 13;
77
let reqNum = 0;
88

99
function test(res, header, code) {
10-
const errRegExp = new RegExp(`^RangeError: Invalid status code: ${code}$`);
10+
const errRegExp = common.expectsError({
11+
code: 'ERR_HTTP_INVALID_STATUS_CODE',
12+
type: RangeError,
13+
message: `Invalid status code: ${code}`
14+
});
1115
assert.throws(() => {
1216
res.writeHead(header);
1317
}, errRegExp);
@@ -25,7 +29,7 @@ const server = http.Server(common.mustCall(function(req, res) {
2529
test(res, NaN, 'NaN');
2630
break;
2731
case 3:
28-
test(res, {}, '\\[object Object\\]');
32+
test(res, {}, '[object Object]');
2933
break;
3034
case 4:
3135
test(res, 99, '99');
@@ -53,7 +57,11 @@ const server = http.Server(common.mustCall(function(req, res) {
5357
break;
5458
case 12:
5559
assert.throws(() => { res.writeHead(); },
56-
/^RangeError: Invalid status code: undefined$/);
60+
common.expectsError({
61+
code: 'ERR_HTTP_INVALID_STATUS_CODE',
62+
type: RangeError,
63+
message: 'Invalid status code: undefined'
64+
}));
5765
this.close();
5866
break;
5967
default:

test/parallel/test-http-write-head.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ const s = http.createServer(common.mustCall((req, res) => {
5656

5757
assert.throws(() => {
5858
res.writeHead(100, {});
59-
}, /^Error: Can't render headers after they are sent to the client$/);
59+
}, common.expectsError({
60+
code: 'ERR_HTTP_HEADERS_SENT',
61+
type: Error,
62+
message: 'Cannot render headers after they are sent to the client'
63+
})
64+
);
6065

6166
res.end();
6267
}));

0 commit comments

Comments
 (0)