Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit a3482b9

Browse files
Use real node errors.
Seeing as this is is supposed to mimick node's underlying file-system, seems to make sense to use the same errors. Also means that it becomes easier to use this to stub out the `fs` module.
1 parent a4d5b02 commit a3482b9

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

lib/MemoryFileSystem.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
*/
55

66
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();
719

820
function MemoryFileSystem(data) {
921
this.data = data || {};
@@ -24,7 +36,9 @@ function pathToArray(path) {
2436
path = normalize(path);
2537
var nix = /^\//.test(path);
2638
if(!nix) {
27-
if(!/^[A-Za-z]:/.test(path)) throw new Error("Invalid path '" + path + "'");
39+
if(!/^[A-Za-z]:/.test(path)) {
40+
throw new MemoryFileSystemError(errors.code.EINVAL, path);
41+
}
2842
path = path.replace(/[\\\/]+/g, "\\"); // multi slashs
2943
path = path.split(/[\\\/]/);
3044
path[0] = path[0].toUpperCase();
@@ -76,23 +90,24 @@ MemoryFileSystem.prototype.statSync = function(_path) {
7690
isFIFO: falseFn,
7791
isSocket: falseFn
7892
};
79-
} else
80-
throw new Error("Path doesn't exist '" + _path + "'");
93+
} else {
94+
throw new MemoryFileSystemError(errors.code.ENOENT, _path);
95+
}
8196
};
8297

8398
MemoryFileSystem.prototype.readFileSync = function(_path, encoding) {
8499
var path = pathToArray(_path);
85100
var current = this.data;
86101
for(var i = 0; i < path.length - 1; i++) {
87102
if(!isDir(current[path[i]]))
88-
throw new Error("Path doesn't exist '" + _path + "'");
103+
throw new MemoryFileSystemError(errors.code.ENOENT, _path);
89104
current = current[path[i]];
90105
}
91106
if(!isFile(current[path[i]])) {
92107
if(isDir(current[path[i]]))
93-
throw new Error("Cannot readFile on directory '" + _path + "'");
108+
throw new MemoryFileSystemError(errors.code.EISDIR, _path);
94109
else
95-
throw new Error("Path doesn't exist '" + _path + "'");
110+
throw new MemoryFileSystemError(errors.code.ENOENT, _path);
96111
}
97112
current = current[path[i]];
98113
return encoding ? current.toString(encoding) : current;
@@ -104,14 +119,14 @@ MemoryFileSystem.prototype.readdirSync = function(_path) {
104119
var current = this.data;
105120
for(var i = 0; i < path.length - 1; i++) {
106121
if(!isDir(current[path[i]]))
107-
throw new Error("Path doesn't exist '" + _path + "'");
122+
throw new MemoryFileSystemError(errors.code.ENOENT, _path);
108123
current = current[path[i]];
109124
}
110125
if(!isDir(current[path[i]])) {
111126
if(isFile(current[path[i]]))
112-
throw new Error("Cannot readdir on file '" + _path + "'");
127+
throw new MemoryFileSystemError(errors.code.ENOTDIR, _path);
113128
else
114-
throw new Error("Path doesn't exist '" + _path + "'");
129+
throw new MemoryFileSystemError(errors.code.ENOENT, _path);
115130
}
116131
return Object.keys(current[path[i]]).filter(Boolean);
117132
};
@@ -122,7 +137,7 @@ MemoryFileSystem.prototype.mkdirpSync = function(_path) {
122137
var current = this.data;
123138
for(var i = 0; i < path.length; i++) {
124139
if(isFile(current[path[i]]))
125-
throw new Error("Path is a file '" + _path + "'");
140+
throw new MemoryFileSystemError(errors.code.ENOTDIR, _path);
126141
else if(!isDir(current[path[i]]))
127142
current[path[i]] = {"":true};
128143
current = current[path[i]];
@@ -136,28 +151,30 @@ MemoryFileSystem.prototype.mkdirSync = function(_path) {
136151
var current = this.data;
137152
for(var i = 0; i < path.length - 1; i++) {
138153
if(!isDir(current[path[i]]))
139-
throw new Error("Path doesn't exist '" + _path + "'");
154+
throw new MemoryFileSystemError(errors.code.ENOENT, _path);
140155
current = current[path[i]];
141156
}
142157
if(isDir(current[path[i]]))
143-
throw new new Error("Directory already exist '" + _path + "'");
158+
throw new MemoryFileSystemError(errors.code.EEXIST, _path);
144159
else if(isFile(current[path[i]]))
145-
throw new Error("Cannot mkdir on file '" + _path + "'");
160+
throw new MemoryFileSystemError(errors.code.ENOTDIR, _path);
146161
current[path[i]] = {"":true};
147162
return;
148163
};
149164

150165
MemoryFileSystem.prototype._remove = function(_path, name, testFn) {
151166
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+
}
153170
var current = this.data;
154171
for(var i = 0; i < path.length - 1; i++) {
155172
if(!isDir(current[path[i]]))
156-
throw new Error("Path doesn't exist '" + _path + "'");
173+
throw new MemoryFileSystemError(errors.code.ENOENT, _path);
157174
current = current[path[i]];
158175
}
159176
if(!testFn(current[path[i]]))
160-
throw new Error("'" + name + "' doesn't exist '" + _path + "'");
177+
throw new MemoryFileSystemError(errors.code.ENOENT, _path);
161178
delete current[path[i]];
162179
return;
163180
};
@@ -171,21 +188,23 @@ MemoryFileSystem.prototype.unlinkSync = function(_path) {
171188
};
172189

173190
MemoryFileSystem.prototype.readlinkSync = function(_path) {
174-
throw new Error("Path is not a link '" + _path + "'");
191+
throw new MemoryFileSystemError(errors.code.ENOSYS, _path);
175192
};
176193

177194
MemoryFileSystem.prototype.writeFileSync = function(_path, content, encoding) {
178195
if(!content && !encoding) throw new Error("No content");
179196
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+
}
181200
var current = this.data;
182201
for(var i = 0; i < path.length - 1; i++) {
183202
if(!isDir(current[path[i]]))
184-
throw new Error("Path doesn't exist '" + _path + "'");
203+
throw new MemoryFileSystemError(errors.code.ENOENT, _path);
185204
current = current[path[i]];
186205
}
187206
if(isDir(current[path[i]]))
188-
throw new Error("Cannot writeFile on directory '" + _path + "'");
207+
throw new MemoryFileSystemError(errors.code.EISDIR, _path);
189208
current[path[i]] = encoding || typeof content === "string" ? new Buffer(content, encoding) : content;
190209
return;
191210
};

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
"url": "https://github.com/webpack/memory-fs/issues"
2626
},
2727
"homepage": "https://github.com/webpack/memory-fs",
28+
"dependencies": {
29+
"errno": "^0.1.3"
30+
},
2831
"devDependencies": {
2932
"codecov.io": "^0.1.4",
3033
"coveralls": "^2.11.2",

0 commit comments

Comments
 (0)