Skip to content

Commit

Permalink
errors,http_server: migrate to use internal/errors.js
Browse files Browse the repository at this point in the history
PR-URL: #13301
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
bidipyne authored and jasnell committed Jun 2, 2017
1 parent 2822796 commit a9f798e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
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

0 comments on commit a9f798e

Please sign in to comment.