Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors,http_server: migrate to use internal/errors.js #13301

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const chunkExpression = common.chunkExpression;
const httpSocketSetup = common.httpSocketSetup;
const OutgoingMessage = require('_http_outgoing').OutgoingMessage;
const { outHeadersKey, ondrain } = require('internal/http');
const errors = require('internal/errors');

const STATUS_CODES = {
100: 'Continue',
Expand Down Expand Up @@ -185,7 +186,9 @@ function writeHead(statusCode, reason, obj) {

statusCode |= 0;
if (statusCode < 100 || statusCode > 999)
throw new RangeError(`Invalid status code: ${originalStatusCode}`);
throw new errors.RangeError('ERR_HTTP_INVALID_STATUS_CODE',
originalStatusCode);


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

if (common._checkInvalidHeaderChar(this.statusMessage))
throw new Error('Invalid character in statusMessage.');
throw new errors.Error('ERR_HTTP_INVALID_CHAR',
'Invalid character in statusMessage.');

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

Expand Down
5 changes: 5 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ E('ERR_ARG_NOT_ITERABLE', '%s must be iterable');
E('ERR_ASSERTION', (msg) => msg);
E('ERR_CONSOLE_WRITABLE_STREAM',
(name) => `Console expects a writable stream instance for ${name}`);
E('ERR_HTTP_HEADERS_SENT',
'Cannot render headers after they are sent to the client');
E('ERR_HTTP_INVALID_CHAR', 'Invalid character in statusMessage.');
E('ERR_HTTP_INVALID_STATUS_CODE',
(originalStatusCode) => `Invalid status code: ${originalStatusCode}`);
E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
E('ERR_INVALID_ARG_TYPE', invalidArgType);
E('ERR_INVALID_CALLBACK', 'callback must be a function');
Expand Down
14 changes: 11 additions & 3 deletions test/parallel/test-http-response-statuscode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const MAX_REQUESTS = 13;
let reqNum = 0;

function test(res, header, code) {
const errRegExp = new RegExp(`^RangeError: Invalid status code: ${code}$`);
const errRegExp = common.expectsError({
code: 'ERR_HTTP_INVALID_STATUS_CODE',
type: RangeError,
message: `Invalid status code: ${code}`
});
assert.throws(() => {
res.writeHead(header);
}, errRegExp);
Expand All @@ -25,7 +29,7 @@ const server = http.Server(common.mustCall(function(req, res) {
test(res, NaN, 'NaN');
break;
case 3:
test(res, {}, '\\[object Object\\]');
test(res, {}, '[object Object]');
break;
case 4:
test(res, 99, '99');
Expand Down Expand Up @@ -53,7 +57,11 @@ const server = http.Server(common.mustCall(function(req, res) {
break;
case 12:
assert.throws(() => { res.writeHead(); },
/^RangeError: Invalid status code: undefined$/);
common.expectsError({
code: 'ERR_HTTP_INVALID_STATUS_CODE',
type: RangeError,
message: 'Invalid status code: undefined'
}));
this.close();
break;
default:
Expand Down
7 changes: 6 additions & 1 deletion test/parallel/test-http-write-head.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ const s = http.createServer(common.mustCall((req, res) => {

assert.throws(() => {
res.writeHead(100, {});
}, /^Error: Can't render headers after they are sent to the client$/);
}, common.expectsError({
code: 'ERR_HTTP_HEADERS_SENT',
type: Error,
message: 'Cannot render headers after they are sent to the client'
})
);

res.end();
}));
Expand Down