@@ -37,6 +37,7 @@ const Buffer = require('buffer').Buffer;
3737const { urlToOptions, searchParamsSymbol } = require ( 'internal/url' ) ;
3838const outHeadersKey = require ( 'internal/http' ) . outHeadersKey ;
3939const nextTick = require ( 'internal/process/next_tick' ) . nextTick ;
40+ const errors = require ( 'internal/errors' ) ;
4041
4142// The actual list of disallowed characters in regexp form is more like:
4243// /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
@@ -68,8 +69,8 @@ function isInvalidPath(s) {
6869
6970function validateHost ( host , name ) {
7071 if ( host != null && typeof host !== 'string' ) {
71- throw new TypeError (
72- `"options. ${ name } " must either be a string, undefined or null` ) ;
72+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , `options. ${ name } ` ,
73+ [ ' string' , ' undefined' , ' null' ] , host ) ;
7374 }
7475 return host ;
7576}
@@ -80,7 +81,7 @@ function ClientRequest(options, cb) {
8081 if ( typeof options === 'string' ) {
8182 options = url . parse ( options ) ;
8283 if ( ! options . hostname ) {
83- throw new Error ( 'Unable to determine the domain name ' ) ;
84+ throw new errors . Error ( 'ERR_INVALID_DOMAIN_NAME ' ) ;
8485 }
8586 } else if ( options && options [ searchParamsSymbol ] &&
8687 options [ searchParamsSymbol ] [ searchParamsSymbol ] ) {
@@ -101,9 +102,8 @@ function ClientRequest(options, cb) {
101102 // Explicitly pass through this statement as agent will not be used
102103 // when createConnection is provided.
103104 } else if ( typeof agent . addRequest !== 'function' ) {
104- throw new TypeError (
105- 'Agent option must be an Agent-like object, undefined, or false.'
106- ) ;
105+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'Agent option' ,
106+ [ 'Agent-like object' , 'undefined' , 'false' ] ) ;
107107 }
108108 this . agent = agent ;
109109
@@ -122,12 +122,11 @@ function ClientRequest(options, cb) {
122122 invalidPath = / [ \u0000 - \u0020 ] / . test ( path ) ;
123123 }
124124 if ( invalidPath )
125- throw new TypeError ( 'Request path contains unescaped characters ' ) ;
125+ throw new errors . TypeError ( 'ERR_UNESCAPED_CHARACTERS' , ' Request path') ;
126126 }
127127
128128 if ( protocol !== expectedProtocol ) {
129- throw new Error ( 'Protocol "' + protocol + '" not supported. ' +
130- 'Expected "' + expectedProtocol + '"' ) ;
129+ throw new errors . Error ( 'ERR_INVALID_PROTOCOL' , protocol , expectedProtocol ) ;
131130 }
132131
133132 var defaultPort = options . defaultPort ||
@@ -145,12 +144,13 @@ function ClientRequest(options, cb) {
145144 var method = options . method ;
146145 var methodIsString = ( typeof method === 'string' ) ;
147146 if ( method != null && ! methodIsString ) {
148- throw new TypeError ( 'Method must be a string' ) ;
147+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'method' ,
148+ 'string' , method ) ;
149149 }
150150
151151 if ( methodIsString && method ) {
152152 if ( ! common . _checkIsHttpToken ( method ) ) {
153- throw new TypeError ( 'Method must be a valid HTTP token ' ) ;
153+ throw new errors . TypeError ( 'ERR_INVALID_HTTP_TOKEN' , 'Method ') ;
154154 }
155155 method = this . method = method . toUpperCase ( ) ;
156156 } else {
@@ -211,8 +211,7 @@ function ClientRequest(options, cb) {
211211 options . headers ) ;
212212 } else if ( this . getHeader ( 'expect' ) ) {
213213 if ( this . _header ) {
214- throw new Error ( 'Can\'t render headers after they are sent to the ' +
215- 'client' ) ;
214+ throw new errors . Error ( 'ERR_HTTP_HEADERS_SENT' ) ;
216215 }
217216
218217 this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' ,
@@ -303,7 +302,7 @@ ClientRequest.prototype._finish = function _finish() {
303302
304303ClientRequest . prototype . _implicitHeader = function _implicitHeader ( ) {
305304 if ( this . _header ) {
306- throw new Error ( 'Can\'t render headers after they are sent to the client ' ) ;
305+ throw new errors . Error ( 'ERR_HTTP_HEADERS_SENT ' ) ;
307306 }
308307 this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' ,
309308 this [ outHeadersKey ] ) ;
0 commit comments