4
4
*/
5
5
6
6
var normalize = require ( "./normalize" ) ;
7
+ var errors = require ( "errno" ) ;
8
+
9
+ function MemoryFileSystemError ( err , path ) {
10
+ Error . call ( this )
11
+ if ( Error . captureStackTrace )
12
+ Error . captureStackTrace ( this , arguments . callee )
13
+ this . code = err . code ;
14
+ this . errno = err . errno ;
15
+ this . message = err . description ;
16
+ this . path = path ;
17
+ }
18
+ MemoryFileSystemError . prototype = new Error ( ) ;
7
19
8
20
function MemoryFileSystem ( data ) {
9
21
this . data = data || { } ;
@@ -24,7 +36,9 @@ function pathToArray(path) {
24
36
path = normalize ( path ) ;
25
37
var nix = / ^ \/ / . test ( path ) ;
26
38
if ( ! nix ) {
27
- if ( ! / ^ [ A - Z a - z ] : / . test ( path ) ) throw new Error ( "Invalid path '" + path + "'" ) ;
39
+ if ( ! / ^ [ A - Z a - z ] : / . test ( path ) ) {
40
+ throw new MemoryFileSystemError ( errors . code . EINVAL , path ) ;
41
+ }
28
42
path = path . replace ( / [ \\ \/ ] + / g, "\\" ) ; // multi slashs
29
43
path = path . split ( / [ \\ \/ ] / ) ;
30
44
path [ 0 ] = path [ 0 ] . toUpperCase ( ) ;
@@ -76,23 +90,24 @@ MemoryFileSystem.prototype.statSync = function(_path) {
76
90
isFIFO : falseFn ,
77
91
isSocket : falseFn
78
92
} ;
79
- } else
80
- throw new Error ( "Path doesn't exist '" + _path + "'" ) ;
93
+ } else {
94
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
95
+ }
81
96
} ;
82
97
83
98
MemoryFileSystem . prototype . readFileSync = function ( _path , encoding ) {
84
99
var path = pathToArray ( _path ) ;
85
100
var current = this . data ;
86
101
for ( var i = 0 ; i < path . length - 1 ; i ++ ) {
87
102
if ( ! isDir ( current [ path [ i ] ] ) )
88
- throw new Error ( "Path doesn't exist '" + _path + "'" ) ;
103
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
89
104
current = current [ path [ i ] ] ;
90
105
}
91
106
if ( ! isFile ( current [ path [ i ] ] ) ) {
92
107
if ( isDir ( current [ path [ i ] ] ) )
93
- throw new Error ( "Cannot readFile on directory '" + _path + "'" ) ;
108
+ throw new MemoryFileSystemError ( errors . code . EISDIR , _path ) ;
94
109
else
95
- throw new Error ( "Path doesn't exist '" + _path + "'" ) ;
110
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
96
111
}
97
112
current = current [ path [ i ] ] ;
98
113
return encoding ? current . toString ( encoding ) : current ;
@@ -104,14 +119,14 @@ MemoryFileSystem.prototype.readdirSync = function(_path) {
104
119
var current = this . data ;
105
120
for ( var i = 0 ; i < path . length - 1 ; i ++ ) {
106
121
if ( ! isDir ( current [ path [ i ] ] ) )
107
- throw new Error ( "Path doesn't exist '" + _path + "'" ) ;
122
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
108
123
current = current [ path [ i ] ] ;
109
124
}
110
125
if ( ! isDir ( current [ path [ i ] ] ) ) {
111
126
if ( isFile ( current [ path [ i ] ] ) )
112
- throw new Error ( "Cannot readdir on file '" + _path + "'" ) ;
127
+ throw new MemoryFileSystemError ( errors . code . ENOTDIR , _path ) ;
113
128
else
114
- throw new Error ( "Path doesn't exist '" + _path + "'" ) ;
129
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
115
130
}
116
131
return Object . keys ( current [ path [ i ] ] ) . filter ( Boolean ) ;
117
132
} ;
@@ -122,7 +137,7 @@ MemoryFileSystem.prototype.mkdirpSync = function(_path) {
122
137
var current = this . data ;
123
138
for ( var i = 0 ; i < path . length ; i ++ ) {
124
139
if ( isFile ( current [ path [ i ] ] ) )
125
- throw new Error ( "Path is a file '" + _path + "'" ) ;
140
+ throw new MemoryFileSystemError ( errors . code . ENOTDIR , _path ) ;
126
141
else if ( ! isDir ( current [ path [ i ] ] ) )
127
142
current [ path [ i ] ] = { "" :true } ;
128
143
current = current [ path [ i ] ] ;
@@ -136,28 +151,30 @@ MemoryFileSystem.prototype.mkdirSync = function(_path) {
136
151
var current = this . data ;
137
152
for ( var i = 0 ; i < path . length - 1 ; i ++ ) {
138
153
if ( ! isDir ( current [ path [ i ] ] ) )
139
- throw new Error ( "Path doesn't exist '" + _path + "'" ) ;
154
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
140
155
current = current [ path [ i ] ] ;
141
156
}
142
157
if ( isDir ( current [ path [ i ] ] ) )
143
- throw new new Error ( "Directory already exist '" + _path + "'" ) ;
158
+ throw new MemoryFileSystemError ( errors . code . EEXIST , _path ) ;
144
159
else if ( isFile ( current [ path [ i ] ] ) )
145
- throw new Error ( "Cannot mkdir on file '" + _path + "'" ) ;
160
+ throw new MemoryFileSystemError ( errors . code . ENOTDIR , _path ) ;
146
161
current [ path [ i ] ] = { "" :true } ;
147
162
return ;
148
163
} ;
149
164
150
165
MemoryFileSystem . prototype . _remove = function ( _path , name , testFn ) {
151
166
var path = pathToArray ( _path ) ;
152
- if ( path . length === 0 ) throw new Error ( "Path cannot be removed '" + _path + "'" ) ;
167
+ if ( path . length === 0 ) {
168
+ throw new MemoryFileSystemError ( errors . code . EPERM , _path ) ;
169
+ }
153
170
var current = this . data ;
154
171
for ( var i = 0 ; i < path . length - 1 ; i ++ ) {
155
172
if ( ! isDir ( current [ path [ i ] ] ) )
156
- throw new Error ( "Path doesn't exist '" + _path + "'" ) ;
173
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
157
174
current = current [ path [ i ] ] ;
158
175
}
159
176
if ( ! testFn ( current [ path [ i ] ] ) )
160
- throw new Error ( "'" + name + "' doesn't exist '" + _path + "'" ) ;
177
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
161
178
delete current [ path [ i ] ] ;
162
179
return ;
163
180
} ;
@@ -171,21 +188,23 @@ MemoryFileSystem.prototype.unlinkSync = function(_path) {
171
188
} ;
172
189
173
190
MemoryFileSystem . prototype . readlinkSync = function ( _path ) {
174
- throw new Error ( "Path is not a link '" + _path + "'" ) ;
191
+ throw new MemoryFileSystemError ( errors . code . ENOSYS , _path ) ;
175
192
} ;
176
193
177
194
MemoryFileSystem . prototype . writeFileSync = function ( _path , content , encoding ) {
178
195
if ( ! content && ! encoding ) throw new Error ( "No content" ) ;
179
196
var path = pathToArray ( _path ) ;
180
- if ( path . length === 0 ) throw new Error ( "Path is not a file '" + _path + "'" ) ;
197
+ if ( path . length === 0 ) {
198
+ throw new MemoryFileSystemError ( errors . code . EISDIR , _path ) ;
199
+ }
181
200
var current = this . data ;
182
201
for ( var i = 0 ; i < path . length - 1 ; i ++ ) {
183
202
if ( ! isDir ( current [ path [ i ] ] ) )
184
- throw new Error ( "Path doesn't exist '" + _path + "'" ) ;
203
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
185
204
current = current [ path [ i ] ] ;
186
205
}
187
206
if ( isDir ( current [ path [ i ] ] ) )
188
- throw new Error ( "Cannot writeFile on directory '" + _path + "'" ) ;
207
+ throw new MemoryFileSystemError ( errors . code . EISDIR , _path ) ;
189
208
current [ path [ i ] ] = encoding || typeof content === "string" ? new Buffer ( content , encoding ) : content ;
190
209
return ;
191
210
} ;
0 commit comments