@@ -37,6 +37,7 @@ const Buffer = require('buffer').Buffer;
37
37
const { urlToOptions, searchParamsSymbol } = require ( 'internal/url' ) ;
38
38
const outHeadersKey = require ( 'internal/http' ) . outHeadersKey ;
39
39
const nextTick = require ( 'internal/process/next_tick' ) . nextTick ;
40
+ const errors = require ( 'internal/errors' ) ;
40
41
41
42
// The actual list of disallowed characters in regexp form is more like:
42
43
// /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
@@ -68,8 +69,8 @@ function isInvalidPath(s) {
68
69
69
70
function validateHost ( host , name ) {
70
71
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 ) ;
73
74
}
74
75
return host ;
75
76
}
@@ -80,7 +81,7 @@ function ClientRequest(options, cb) {
80
81
if ( typeof options === 'string' ) {
81
82
options = url . parse ( options ) ;
82
83
if ( ! options . hostname ) {
83
- throw new Error ( 'Unable to determine the domain name ' ) ;
84
+ throw new errors . Error ( 'ERR_INVALID_DOMAIN_NAME ' ) ;
84
85
}
85
86
} else if ( options && options [ searchParamsSymbol ] &&
86
87
options [ searchParamsSymbol ] [ searchParamsSymbol ] ) {
@@ -101,9 +102,8 @@ function ClientRequest(options, cb) {
101
102
// Explicitly pass through this statement as agent will not be used
102
103
// when createConnection is provided.
103
104
} 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' ] ) ;
107
107
}
108
108
this . agent = agent ;
109
109
@@ -122,12 +122,11 @@ function ClientRequest(options, cb) {
122
122
invalidPath = / [ \u0000 - \u0020 ] / . test ( path ) ;
123
123
}
124
124
if ( invalidPath )
125
- throw new TypeError ( 'Request path contains unescaped characters ' ) ;
125
+ throw new errors . TypeError ( 'ERR_UNESCAPED_CHARACTERS' , ' Request path') ;
126
126
}
127
127
128
128
if ( protocol !== expectedProtocol ) {
129
- throw new Error ( 'Protocol "' + protocol + '" not supported. ' +
130
- 'Expected "' + expectedProtocol + '"' ) ;
129
+ throw new errors . Error ( 'ERR_INVALID_PROTOCOL' , protocol , expectedProtocol ) ;
131
130
}
132
131
133
132
var defaultPort = options . defaultPort ||
@@ -145,12 +144,13 @@ function ClientRequest(options, cb) {
145
144
var method = options . method ;
146
145
var methodIsString = ( typeof method === 'string' ) ;
147
146
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 ) ;
149
149
}
150
150
151
151
if ( methodIsString && method ) {
152
152
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 ') ;
154
154
}
155
155
method = this . method = method . toUpperCase ( ) ;
156
156
} else {
@@ -211,8 +211,7 @@ function ClientRequest(options, cb) {
211
211
options . headers ) ;
212
212
} else if ( this . getHeader ( 'expect' ) ) {
213
213
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' ) ;
216
215
}
217
216
218
217
this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' ,
@@ -303,7 +302,7 @@ ClientRequest.prototype._finish = function _finish() {
303
302
304
303
ClientRequest . prototype . _implicitHeader = function _implicitHeader ( ) {
305
304
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 ' ) ;
307
306
}
308
307
this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' ,
309
308
this [ outHeadersKey ] ) ;
0 commit comments