@@ -108,6 +108,20 @@ function copyObject(source) {
108
108
return target ;
109
109
}
110
110
111
+ function handleErrorFromBinding ( ctx ) {
112
+ if ( ctx . errno !== undefined ) { // libuv error numbers
113
+ const err = errors . uvException ( ctx ) ;
114
+ Error . captureStackTrace ( err , handleErrorFromBinding ) ;
115
+ throw err ;
116
+ } else if ( ctx . error !== undefined ) { // errors created in C++ land.
117
+ // TODO(joyeecheung): currently, ctx.error are encoding errors
118
+ // usually caused by memory problems. We need to figure out proper error
119
+ // code(s) for this.
120
+ Error . captureStackTrace ( ctx . error , handleErrorFromBinding ) ;
121
+ throw ctx . error ;
122
+ }
123
+ }
124
+
111
125
// TODO(joyeecheung): explore how the deprecation could be solved via linting
112
126
// rules. See https://github.com/nodejs/node/pull/12976
113
127
function rethrow ( ) {
@@ -405,10 +419,7 @@ fs.accessSync = function(path, mode) {
405
419
406
420
const ctx = { path } ;
407
421
binding . access ( pathModule . toNamespacedPath ( path ) , mode , undefined , ctx ) ;
408
-
409
- if ( ctx . errno !== undefined ) {
410
- throw errors . uvException ( ctx ) ;
411
- }
422
+ handleErrorFromBinding ( ctx ) ;
412
423
} ;
413
424
414
425
// fs.exists never throws even when the arguments are invalid - if there is
@@ -742,9 +753,7 @@ fs.closeSync = function(fd) {
742
753
743
754
const ctx = { } ;
744
755
binding . close ( fd , undefined , ctx ) ;
745
- if ( ctx . errno !== undefined ) {
746
- throw errors . uvException ( ctx ) ;
747
- }
756
+ handleErrorFromBinding ( ctx ) ;
748
757
} ;
749
758
750
759
function modeNum ( m , def ) {
@@ -924,9 +933,7 @@ fs.renameSync = function(oldPath, newPath) {
924
933
const ctx = { path : oldPath , dest : newPath } ;
925
934
binding . rename ( pathModule . toNamespacedPath ( oldPath ) ,
926
935
pathModule . toNamespacedPath ( newPath ) , undefined , ctx ) ;
927
- if ( ctx . errno !== undefined ) {
928
- throw errors . uvException ( ctx ) ;
929
- }
936
+ handleErrorFromBinding ( ctx ) ;
930
937
} ;
931
938
932
939
fs . truncate = function ( path , len , callback ) {
@@ -994,9 +1001,7 @@ fs.ftruncateSync = function(fd, len = 0) {
994
1001
len = Math . max ( 0 , len ) ;
995
1002
const ctx = { } ;
996
1003
binding . ftruncate ( fd , len , undefined , ctx ) ;
997
- if ( ctx . errno !== undefined ) {
998
- throw errors . uvException ( ctx ) ;
999
- }
1004
+ handleErrorFromBinding ( ctx ) ;
1000
1005
} ;
1001
1006
1002
1007
fs . rmdir = function ( path , callback ) {
@@ -1025,9 +1030,7 @@ fs.fdatasyncSync = function(fd) {
1025
1030
validateUint32 ( fd , 'fd' ) ;
1026
1031
const ctx = { } ;
1027
1032
binding . fdatasync ( fd , undefined , ctx ) ;
1028
- if ( ctx . errno !== undefined ) {
1029
- throw errors . uvException ( ctx ) ;
1030
- }
1033
+ handleErrorFromBinding ( ctx ) ;
1031
1034
} ;
1032
1035
1033
1036
fs . fsync = function ( fd , callback ) {
@@ -1041,9 +1044,7 @@ fs.fsyncSync = function(fd) {
1041
1044
validateUint32 ( fd , 'fd' ) ;
1042
1045
const ctx = { } ;
1043
1046
binding . fsync ( fd , undefined , ctx ) ;
1044
- if ( ctx . errno !== undefined ) {
1045
- throw errors . uvException ( ctx ) ;
1046
- }
1047
+ handleErrorFromBinding ( ctx ) ;
1047
1048
} ;
1048
1049
1049
1050
fs . mkdir = function ( path , mode , callback ) {
@@ -1114,9 +1115,7 @@ fs.fstatSync = function(fd) {
1114
1115
validateUint32 ( fd , 'fd' ) ;
1115
1116
const ctx = { fd } ;
1116
1117
binding . fstat ( fd , undefined , ctx ) ;
1117
- if ( ctx . errno !== undefined ) {
1118
- throw errors . uvException ( ctx ) ;
1119
- }
1118
+ handleErrorFromBinding ( ctx ) ;
1120
1119
return statsFromValues ( ) ;
1121
1120
} ;
1122
1121
@@ -1125,9 +1124,7 @@ fs.lstatSync = function(path) {
1125
1124
validatePath ( path ) ;
1126
1125
const ctx = { path } ;
1127
1126
binding . lstat ( pathModule . toNamespacedPath ( path ) , undefined , ctx ) ;
1128
- if ( ctx . errno !== undefined ) {
1129
- throw errors . uvException ( ctx ) ;
1130
- }
1127
+ handleErrorFromBinding ( ctx ) ;
1131
1128
return statsFromValues ( ) ;
1132
1129
} ;
1133
1130
@@ -1136,9 +1133,7 @@ fs.statSync = function(path) {
1136
1133
validatePath ( path ) ;
1137
1134
const ctx = { path } ;
1138
1135
binding . stat ( pathModule . toNamespacedPath ( path ) , undefined , ctx ) ;
1139
- if ( ctx . errno !== undefined ) {
1140
- throw errors . uvException ( ctx ) ;
1141
- }
1136
+ handleErrorFromBinding ( ctx ) ;
1142
1137
return statsFromValues ( ) ;
1143
1138
} ;
1144
1139
@@ -1159,14 +1154,7 @@ fs.readlinkSync = function(path, options) {
1159
1154
const ctx = { path } ;
1160
1155
const result = binding . readlink ( pathModule . toNamespacedPath ( path ) ,
1161
1156
options . encoding , undefined , ctx ) ;
1162
- if ( ctx . errno !== undefined ) {
1163
- throw errors . uvException ( ctx ) ;
1164
- } else if ( ctx . error ) {
1165
- // TODO(joyeecheung): this is an encoding error usually caused by memory
1166
- // problems. We need to figure out proper error code(s) for this.
1167
- Error . captureStackTrace ( ctx . error ) ;
1168
- throw ctx . error ;
1169
- }
1157
+ handleErrorFromBinding ( ctx ) ;
1170
1158
return result ;
1171
1159
} ;
1172
1160
@@ -1235,9 +1223,7 @@ fs.symlinkSync = function(target, path, type) {
1235
1223
binding . symlink ( preprocessSymlinkDestination ( target , type , path ) ,
1236
1224
pathModule . toNamespacedPath ( path ) , flags , undefined , ctx ) ;
1237
1225
1238
- if ( ctx . errno !== undefined ) {
1239
- throw errors . uvException ( ctx ) ;
1240
- }
1226
+ handleErrorFromBinding ( ctx ) ;
1241
1227
} ;
1242
1228
1243
1229
fs . link = function ( existingPath , newPath , callback ) {
@@ -1266,9 +1252,7 @@ fs.linkSync = function(existingPath, newPath) {
1266
1252
const result = binding . link ( pathModule . toNamespacedPath ( existingPath ) ,
1267
1253
pathModule . toNamespacedPath ( newPath ) ,
1268
1254
undefined , ctx ) ;
1269
- if ( ctx . errno !== undefined ) {
1270
- throw errors . uvException ( ctx ) ;
1271
- }
1255
+ handleErrorFromBinding ( ctx ) ;
1272
1256
return result ;
1273
1257
} ;
1274
1258
@@ -1286,9 +1270,7 @@ fs.unlinkSync = function(path) {
1286
1270
validatePath ( path ) ;
1287
1271
const ctx = { path } ;
1288
1272
binding . unlink ( pathModule . toNamespacedPath ( path ) , undefined , ctx ) ;
1289
- if ( ctx . errno !== undefined ) {
1290
- throw errors . uvException ( ctx ) ;
1291
- }
1273
+ handleErrorFromBinding ( ctx ) ;
1292
1274
} ;
1293
1275
1294
1276
fs . fchmod = function ( fd , mode , callback ) {
@@ -1887,9 +1869,7 @@ fs.realpathSync = function realpathSync(p, options) {
1887
1869
if ( isWindows && ! knownHard [ base ] ) {
1888
1870
const ctx = { path : base } ;
1889
1871
binding . lstat ( pathModule . toNamespacedPath ( base ) , undefined , ctx ) ;
1890
- if ( ctx . errno !== undefined ) {
1891
- throw errors . uvException ( ctx ) ;
1892
- }
1872
+ handleErrorFromBinding ( ctx ) ;
1893
1873
knownHard [ base ] = true ;
1894
1874
}
1895
1875
@@ -1931,9 +1911,7 @@ fs.realpathSync = function realpathSync(p, options) {
1931
1911
var baseLong = pathModule . toNamespacedPath ( base ) ;
1932
1912
const ctx = { path : base } ;
1933
1913
binding . lstat ( baseLong , undefined , ctx ) ;
1934
- if ( ctx . errno !== undefined ) {
1935
- throw errors . uvException ( ctx ) ;
1936
- }
1914
+ handleErrorFromBinding ( ctx ) ;
1937
1915
1938
1916
if ( ( statValues [ 1 /*mode*/ ] & S_IFMT ) !== S_IFLNK ) {
1939
1917
knownHard [ base ] = true ;
@@ -1956,13 +1934,9 @@ fs.realpathSync = function realpathSync(p, options) {
1956
1934
if ( linkTarget === null ) {
1957
1935
const ctx = { path : base } ;
1958
1936
binding . stat ( baseLong , undefined , ctx ) ;
1959
- if ( ctx . errno !== undefined ) {
1960
- throw errors . uvException ( ctx ) ;
1961
- }
1937
+ handleErrorFromBinding ( ctx ) ;
1962
1938
linkTarget = binding . readlink ( baseLong , undefined , undefined , ctx ) ;
1963
- if ( ctx . errno !== undefined ) {
1964
- throw errors . uvException ( ctx ) ;
1965
- }
1939
+ handleErrorFromBinding ( ctx ) ;
1966
1940
}
1967
1941
resolvedLink = pathModule . resolve ( previous , linkTarget ) ;
1968
1942
@@ -1981,9 +1955,7 @@ fs.realpathSync = function realpathSync(p, options) {
1981
1955
if ( isWindows && ! knownHard [ base ] ) {
1982
1956
const ctx = { path : base } ;
1983
1957
binding . lstat ( pathModule . toNamespacedPath ( base ) , undefined , ctx ) ;
1984
- if ( ctx . errno !== undefined ) {
1985
- throw errors . uvException ( ctx ) ;
1986
- }
1958
+ handleErrorFromBinding ( ctx ) ;
1987
1959
knownHard [ base ] = true ;
1988
1960
}
1989
1961
}
0 commit comments