@@ -33,6 +33,7 @@ const { isUint8Array, createPromise, promiseResolve } = process.binding('util');
3333const binding = process . binding ( 'fs' ) ;
3434const fs = exports ;
3535const Buffer = require ( 'buffer' ) . Buffer ;
36+ const errors = require ( 'internal/errors' ) ;
3637const Stream = require ( 'stream' ) . Stream ;
3738const EventEmitter = require ( 'events' ) ;
3839const FSReqWrap = binding . FSReqWrap ;
@@ -72,8 +73,10 @@ function getOptions(options, defaultOptions) {
7273 defaultOptions . encoding = options ;
7374 options = defaultOptions ;
7475 } 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 ) ;
7780 }
7881
7982 if ( options . encoding !== 'buffer' )
@@ -128,7 +131,7 @@ function makeCallback(cb) {
128131 }
129132
130133 if ( typeof cb !== 'function' ) {
131- throw new TypeError ( '"callback" argument must be a function ' ) ;
134+ throw new errors . TypeError ( 'ERR_INVALID_CALLBACK ' ) ;
132135 }
133136
134137 return function ( ) {
@@ -145,7 +148,7 @@ function makeStatsCallback(cb) {
145148 }
146149
147150 if ( typeof cb !== 'function' ) {
148- throw new TypeError ( '"callback" argument must be a function ' ) ;
151+ throw new errors . TypeError ( 'ERR_INVALID_CALLBACK ' ) ;
149152 }
150153
151154 return function ( err ) {
@@ -156,8 +159,11 @@ function makeStatsCallback(cb) {
156159
157160function nullCheck ( path , callback ) {
158161 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+
161167 if ( typeof callback !== 'function' )
162168 throw er ;
163169 process . nextTick ( callback , er ) ;
@@ -274,7 +280,7 @@ fs.access = function(path, mode, callback) {
274280 callback = mode ;
275281 mode = fs . F_OK ;
276282 } else if ( typeof callback !== 'function' ) {
277- throw new TypeError ( '"callback" argument must be a function ' ) ;
283+ throw new errors . TypeError ( 'ERR_INVALID_CALLBACK ' ) ;
278284 }
279285
280286 if ( handleError ( ( path = getPathFromURL ( path ) ) , callback ) )
@@ -1193,7 +1199,10 @@ function toUnixTimestamp(time) {
11931199 // convert to 123.456 UNIX timestamp
11941200 return time . getTime ( ) / 1000 ;
11951201 }
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 ) ;
11971206}
11981207
11991208// exported for unit tests, not for public consumption
@@ -1495,7 +1504,10 @@ fs.watchFile = function(filename, options, listener) {
14951504 }
14961505
14971506 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 ) ;
14991511 }
15001512
15011513 stat = statWatchers . get ( filename ) ;
@@ -1842,8 +1854,12 @@ fs.realpath = function realpath(p, options, callback) {
18421854fs . mkdtemp = function ( prefix , options , callback ) {
18431855 callback = makeCallback ( typeof options === 'function' ? options : callback ) ;
18441856 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+ }
18471863 if ( ! nullCheck ( prefix , callback ) ) {
18481864 return ;
18491865 }
@@ -1856,8 +1872,12 @@ fs.mkdtemp = function(prefix, options, callback) {
18561872
18571873
18581874fs . 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+ }
18611881 options = getOptions ( options , { } ) ;
18621882 nullCheck ( prefix ) ;
18631883 return binding . mkdtemp ( prefix + 'XXXXXX' , options . encoding ) ;
@@ -1903,16 +1923,26 @@ function ReadStream(path, options) {
19031923
19041924 if ( this . start !== undefined ) {
19051925 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 ) ;
19071930 }
19081931 if ( this . end === undefined ) {
19091932 this . end = Infinity ;
19101933 } 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 ) ;
19121938 }
19131939
19141940 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 ) ;
19161946 }
19171947
19181948 this . pos = this . start ;
@@ -2069,10 +2099,17 @@ function WriteStream(path, options) {
20692099
20702100 if ( this . start !== undefined ) {
20712101 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 ) ;
20732106 }
20742107 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 ) ;
20762113 }
20772114
20782115 this . pos = this . start ;
0 commit comments