@@ -33,7 +33,17 @@ const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
3333const { outHeadersKey } = require ( 'internal/http' ) ;
3434const { async_id_symbol } = require ( 'internal/async_hooks' ) . symbols ;
3535const { nextTick } = require ( 'internal/process/next_tick' ) ;
36- const errors = require ( 'internal/errors' ) ;
36+ const {
37+ ERR_HTTP_HEADERS_SENT ,
38+ ERR_HTTP_INVALID_HEADER_VALUE ,
39+ ERR_HTTP_TRAILER_INVALID ,
40+ ERR_INVALID_HTTP_TOKEN ,
41+ ERR_INVALID_ARG_TYPE ,
42+ ERR_INVALID_CHAR ,
43+ ERR_METHOD_NOT_IMPLEMENTED ,
44+ ERR_STREAM_CANNOT_PIPE ,
45+ ERR_STREAM_WRITE_AFTER_END
46+ } = require ( 'internal/errors' ) . codes ;
3747
3848const { CRLF , debug } = common ;
3949const { utcDate } = internalHttp ;
@@ -165,7 +175,7 @@ Object.defineProperty(OutgoingMessage.prototype, '_headerNames', {
165175
166176OutgoingMessage . prototype . _renderHeaders = function _renderHeaders ( ) {
167177 if ( this . _header ) {
168- throw new errors . Error ( ' ERR_HTTP_HEADERS_SENT' , 'render' ) ;
178+ throw new ERR_HTTP_HEADERS_SENT ( 'render' ) ;
169179 }
170180
171181 var headersMap = this [ outHeadersKey ] ;
@@ -424,7 +434,7 @@ function _storeHeader(firstLine, headers) {
424434 // header fields, regardless of the header fields present in the
425435 // message, and thus cannot contain a message body or 'trailers'.
426436 if ( this . chunkedEncoding !== true && state . trailer ) {
427- throw new errors . Error ( ' ERR_HTTP_TRAILER_INVALID' ) ;
437+ throw new ERR_HTTP_TRAILER_INVALID ( ) ;
428438 }
429439
430440 this . _header = state . header + CRLF ;
@@ -488,12 +498,12 @@ function matchHeader(self, state, field, value) {
488498function validateHeader ( name , value ) {
489499 let err ;
490500 if ( typeof name !== 'string' || ! name || ! checkIsHttpToken ( name ) ) {
491- err = new errors . TypeError ( ' ERR_INVALID_HTTP_TOKEN' , 'Header name' , name ) ;
501+ err = new ERR_INVALID_HTTP_TOKEN ( 'Header name' , name ) ;
492502 } else if ( value === undefined ) {
493- err = new errors . TypeError ( ' ERR_HTTP_INVALID_HEADER_VALUE' , value , name ) ;
503+ err = new ERR_HTTP_INVALID_HEADER_VALUE ( value , name ) ;
494504 } else if ( checkInvalidHeaderChar ( value ) ) {
495505 debug ( 'Header "%s" contains invalid characters' , name ) ;
496- err = new errors . TypeError ( ' ERR_INVALID_CHAR' , 'header content' , name ) ;
506+ err = new ERR_INVALID_CHAR ( 'header content' , name ) ;
497507 }
498508 if ( err !== undefined ) {
499509 Error . captureStackTrace ( err , validateHeader ) ;
@@ -503,7 +513,7 @@ function validateHeader(name, value) {
503513
504514OutgoingMessage . prototype . setHeader = function setHeader ( name , value ) {
505515 if ( this . _header ) {
506- throw new errors . Error ( ' ERR_HTTP_HEADERS_SENT' , 'set' ) ;
516+ throw new ERR_HTTP_HEADERS_SENT ( 'set' ) ;
507517 }
508518 validateHeader ( name , value ) ;
509519
@@ -529,7 +539,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
529539
530540OutgoingMessage . prototype . getHeader = function getHeader ( name ) {
531541 if ( typeof name !== 'string' ) {
532- throw new errors . TypeError ( ' ERR_INVALID_ARG_TYPE' , 'name' , 'string' ) ;
542+ throw new ERR_INVALID_ARG_TYPE ( 'name' , 'string' ) ;
533543 }
534544
535545 if ( ! this [ outHeadersKey ] ) return ;
@@ -565,7 +575,7 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() {
565575
566576OutgoingMessage . prototype . hasHeader = function hasHeader ( name ) {
567577 if ( typeof name !== 'string' ) {
568- throw new errors . TypeError ( ' ERR_INVALID_ARG_TYPE' , 'name' , 'string' ) ;
578+ throw new ERR_INVALID_ARG_TYPE ( 'name' , 'string' ) ;
569579 }
570580
571581 return ! ! ( this [ outHeadersKey ] && this [ outHeadersKey ] [ name . toLowerCase ( ) ] ) ;
@@ -574,11 +584,11 @@ OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
574584
575585OutgoingMessage . prototype . removeHeader = function removeHeader ( name ) {
576586 if ( typeof name !== 'string' ) {
577- throw new errors . TypeError ( ' ERR_INVALID_ARG_TYPE' , 'name' , 'string' ) ;
587+ throw new ERR_INVALID_ARG_TYPE ( 'name' , 'string' ) ;
578588 }
579589
580590 if ( this . _header ) {
581- throw new errors . Error ( ' ERR_HTTP_HEADERS_SENT' , 'remove' ) ;
591+ throw new ERR_HTTP_HEADERS_SENT ( 'remove' ) ;
582592 }
583593
584594 var key = name . toLowerCase ( ) ;
@@ -605,7 +615,7 @@ OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
605615
606616
607617OutgoingMessage . prototype . _implicitHeader = function _implicitHeader ( ) {
608- throw new errors . Error ( ' ERR_METHOD_NOT_IMPLEMENTED' , '_implicitHeader()' ) ;
618+ throw new ERR_METHOD_NOT_IMPLEMENTED ( '_implicitHeader()' ) ;
609619} ;
610620
611621Object . defineProperty ( OutgoingMessage . prototype , 'headersSent' , {
@@ -622,7 +632,7 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
622632
623633function write_ ( msg , chunk , encoding , callback , fromEnd ) {
624634 if ( msg . finished ) {
625- const err = new errors . Error ( ' ERR_STREAM_WRITE_AFTER_END' ) ;
635+ const err = new ERR_STREAM_WRITE_AFTER_END ( ) ;
626636 nextTick ( msg . socket && msg . socket [ async_id_symbol ] ,
627637 writeAfterEndNT . bind ( msg ) ,
628638 err ,
@@ -642,8 +652,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
642652 }
643653
644654 if ( ! fromEnd && typeof chunk !== 'string' && ! ( chunk instanceof Buffer ) ) {
645- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'first argument' ,
646- [ 'string' , 'Buffer' ] ) ;
655+ throw new ERR_INVALID_ARG_TYPE ( 'first argument' , [ 'string' , 'Buffer' ] ) ;
647656 }
648657
649658
@@ -711,12 +720,11 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
711720 value = headers [ key ] ;
712721 }
713722 if ( typeof field !== 'string' || ! field || ! checkIsHttpToken ( field ) ) {
714- throw new errors . TypeError ( 'ERR_INVALID_HTTP_TOKEN' , 'Trailer name' ,
715- field ) ;
723+ throw new ERR_INVALID_HTTP_TOKEN ( 'Trailer name' , field ) ;
716724 }
717725 if ( checkInvalidHeaderChar ( value ) ) {
718726 debug ( 'Trailer "%s" contains invalid characters' , field ) ;
719- throw new errors . TypeError ( ' ERR_INVALID_CHAR' , 'trailer content' , field ) ;
727+ throw new ERR_INVALID_CHAR ( 'trailer content' , field ) ;
720728 }
721729 this . _trailer += field + ': ' + escapeHeaderValue ( value ) + CRLF ;
722730 }
@@ -742,8 +750,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
742750 var uncork ;
743751 if ( chunk ) {
744752 if ( typeof chunk !== 'string' && ! ( chunk instanceof Buffer ) ) {
745- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'first argument' ,
746- [ 'string' , 'Buffer' ] ) ;
753+ throw new ERR_INVALID_ARG_TYPE ( 'first argument' , [ 'string' , 'Buffer' ] ) ;
747754 }
748755 if ( ! this . _header ) {
749756 if ( typeof chunk === 'string' )
@@ -874,7 +881,7 @@ OutgoingMessage.prototype.flush = internalUtil.deprecate(function() {
874881
875882OutgoingMessage . prototype . pipe = function pipe ( ) {
876883 // OutgoingMessage should be write-only. Piping from it is disabled.
877- this . emit ( 'error' , new errors . Error ( ' ERR_STREAM_CANNOT_PIPE' ) ) ;
884+ this . emit ( 'error' , new ERR_STREAM_CANNOT_PIPE ( ) ) ;
878885} ;
879886
880887module . exports = {
0 commit comments