@@ -33,6 +33,7 @@ const { isUint8Array, createPromise, promiseResolve } = process.binding('util');
33
33
const binding = process . binding ( 'fs' ) ;
34
34
const fs = exports ;
35
35
const Buffer = require ( 'buffer' ) . Buffer ;
36
+ const errors = require ( 'internal/errors' ) ;
36
37
const Stream = require ( 'stream' ) . Stream ;
37
38
const EventEmitter = require ( 'events' ) ;
38
39
const FSReqWrap = binding . FSReqWrap ;
@@ -72,8 +73,10 @@ function getOptions(options, defaultOptions) {
72
73
defaultOptions . encoding = options ;
73
74
options = defaultOptions ;
74
75
} else if ( typeof options !== 'object' ) {
75
- throw new TypeError ( '"options" must be a string or an object, got ' +
76
- typeof options + ' instead.' ) ;
76
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
77
+ 'options' ,
78
+ [ 'string' , 'object' ] ,
79
+ options ) ;
77
80
}
78
81
79
82
if ( options . encoding !== 'buffer' )
@@ -128,7 +131,7 @@ function makeCallback(cb) {
128
131
}
129
132
130
133
if ( typeof cb !== 'function' ) {
131
- throw new TypeError ( '"callback" argument must be a function ' ) ;
134
+ throw new errors . TypeError ( 'ERR_INVALID_CALLBACK ' ) ;
132
135
}
133
136
134
137
return function ( ) {
@@ -145,7 +148,7 @@ function makeStatsCallback(cb) {
145
148
}
146
149
147
150
if ( typeof cb !== 'function' ) {
148
- throw new TypeError ( '"callback" argument must be a function ' ) ;
151
+ throw new errors . TypeError ( 'ERR_INVALID_CALLBACK ' ) ;
149
152
}
150
153
151
154
return function ( err ) {
@@ -156,8 +159,11 @@ function makeStatsCallback(cb) {
156
159
157
160
function nullCheck ( path , callback ) {
158
161
if ( ( '' + path ) . indexOf ( '\u0000' ) !== - 1 ) {
159
- var er = new Error ( 'Path must be a string without null bytes' ) ;
160
- er . code = 'ENOENT' ;
162
+ const er = new errors . Error ( 'ERR_INVALID_ARG_TYPE' ,
163
+ 'path' ,
164
+ 'string without null bytes' ,
165
+ path ) ;
166
+
161
167
if ( typeof callback !== 'function' )
162
168
throw er ;
163
169
process . nextTick ( callback , er ) ;
@@ -274,7 +280,7 @@ fs.access = function(path, mode, callback) {
274
280
callback = mode ;
275
281
mode = fs . F_OK ;
276
282
} else if ( typeof callback !== 'function' ) {
277
- throw new TypeError ( '"callback" argument must be a function ' ) ;
283
+ throw new errors . TypeError ( 'ERR_INVALID_CALLBACK ' ) ;
278
284
}
279
285
280
286
if ( handleError ( ( path = getPathFromURL ( path ) ) , callback ) )
@@ -1193,7 +1199,10 @@ function toUnixTimestamp(time) {
1193
1199
// convert to 123.456 UNIX timestamp
1194
1200
return time . getTime ( ) / 1000 ;
1195
1201
}
1196
- throw new Error ( 'Cannot parse time: ' + time ) ;
1202
+ throw new errors . Error ( 'ERR_INVALID_ARG_TYPE' ,
1203
+ 'time' ,
1204
+ [ 'Date' , 'time in seconds' ] ,
1205
+ time ) ;
1197
1206
}
1198
1207
1199
1208
// exported for unit tests, not for public consumption
@@ -1495,7 +1504,10 @@ fs.watchFile = function(filename, options, listener) {
1495
1504
}
1496
1505
1497
1506
if ( typeof listener !== 'function' ) {
1498
- throw new Error ( '"watchFile()" requires a listener function' ) ;
1507
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
1508
+ 'listener' ,
1509
+ 'function' ,
1510
+ listener ) ;
1499
1511
}
1500
1512
1501
1513
stat = statWatchers . get ( filename ) ;
@@ -1842,8 +1854,12 @@ fs.realpath = function realpath(p, options, callback) {
1842
1854
fs . mkdtemp = function ( prefix , options , callback ) {
1843
1855
callback = makeCallback ( typeof options === 'function' ? options : callback ) ;
1844
1856
options = getOptions ( options , { } ) ;
1845
- if ( ! prefix || typeof prefix !== 'string' )
1846
- throw new TypeError ( 'filename prefix is required' ) ;
1857
+ if ( ! prefix || typeof prefix !== 'string' ) {
1858
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
1859
+ 'prefix' ,
1860
+ 'string' ,
1861
+ prefix ) ;
1862
+ }
1847
1863
if ( ! nullCheck ( prefix , callback ) ) {
1848
1864
return ;
1849
1865
}
@@ -1856,8 +1872,12 @@ fs.mkdtemp = function(prefix, options, callback) {
1856
1872
1857
1873
1858
1874
fs . mkdtempSync = function ( prefix , options ) {
1859
- if ( ! prefix || typeof prefix !== 'string' )
1860
- throw new TypeError ( 'filename prefix is required' ) ;
1875
+ if ( ! prefix || typeof prefix !== 'string' ) {
1876
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
1877
+ 'prefix' ,
1878
+ 'string' ,
1879
+ prefix ) ;
1880
+ }
1861
1881
options = getOptions ( options , { } ) ;
1862
1882
nullCheck ( prefix ) ;
1863
1883
return binding . mkdtemp ( prefix + 'XXXXXX' , options . encoding ) ;
@@ -1903,16 +1923,26 @@ function ReadStream(path, options) {
1903
1923
1904
1924
if ( this . start !== undefined ) {
1905
1925
if ( typeof this . start !== 'number' ) {
1906
- throw new TypeError ( '"start" option must be a Number' ) ;
1926
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
1927
+ 'start' ,
1928
+ 'number' ,
1929
+ this . start ) ;
1907
1930
}
1908
1931
if ( this . end === undefined ) {
1909
1932
this . end = Infinity ;
1910
1933
} else if ( typeof this . end !== 'number' ) {
1911
- throw new TypeError ( '"end" option must be a Number' ) ;
1934
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
1935
+ 'end' ,
1936
+ 'number' ,
1937
+ this . end ) ;
1912
1938
}
1913
1939
1914
1940
if ( this . start > this . end ) {
1915
- throw new Error ( '"start" option must be <= "end" option' ) ;
1941
+ const errVal = `{start: ${ this . start } , end: ${ this . end } }` ;
1942
+ throw new errors . RangeError ( 'ERR_VALUE_OUT_OF_RANGE' ,
1943
+ 'start' ,
1944
+ '<= "end"' ,
1945
+ errVal ) ;
1916
1946
}
1917
1947
1918
1948
this . pos = this . start ;
@@ -2069,10 +2099,17 @@ function WriteStream(path, options) {
2069
2099
2070
2100
if ( this . start !== undefined ) {
2071
2101
if ( typeof this . start !== 'number' ) {
2072
- throw new TypeError ( '"start" option must be a Number' ) ;
2102
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
2103
+ 'start' ,
2104
+ 'number' ,
2105
+ this . start ) ;
2073
2106
}
2074
2107
if ( this . start < 0 ) {
2075
- throw new Error ( '"start" must be >= zero' ) ;
2108
+ const errVal = `{start: ${ this . start } }` ;
2109
+ throw new errors . RangeError ( 'ERR_VALUE_OUT_OF_RANGE' ,
2110
+ 'start' ,
2111
+ '>= 0' ,
2112
+ errVal ) ;
2076
2113
}
2077
2114
2078
2115
this . pos = this . start ;
0 commit comments