From a9f798ebcc1bbef86cff3b0ba8cb3bc004602387 Mon Sep 17 00:00:00 2001 From: Bidisha Pyne Date: Tue, 30 May 2017 10:47:35 -0400 Subject: [PATCH] errors,http_server: migrate to use internal/errors.js PR-URL: https://github.com/nodejs/node/pull/13301 Reviewed-By: Michael Dawson Reviewed-By: James M Snell --- lib/_http_server.js | 11 +++++++---- lib/internal/errors.js | 5 +++++ test/parallel/test-http-response-statuscode.js | 14 +++++++++++--- test/parallel/test-http-write-head.js | 7 ++++++- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index 9143fa1fc46bcb..357400e3501228 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -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', @@ -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]) @@ -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 @@ -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; diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 32fe4a4538f909..8bd7553c89fdf1 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -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'); diff --git a/test/parallel/test-http-response-statuscode.js b/test/parallel/test-http-response-statuscode.js index 1abbe67d80a66d..17fce6c2d0f11e 100644 --- a/test/parallel/test-http-response-statuscode.js +++ b/test/parallel/test-http-response-statuscode.js @@ -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); @@ -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'); @@ -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: diff --git a/test/parallel/test-http-write-head.js b/test/parallel/test-http-write-head.js index dd716f17aef423..4aa1a22012ad25 100644 --- a/test/parallel/test-http-write-head.js +++ b/test/parallel/test-http-write-head.js @@ -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(); }));