Skip to content

Commit dda2727

Browse files
authored
Merge pull request #191 from tapjs/master
Be resilient against readFileSync failures
2 parents d541590 + 43d0b80 commit dda2727

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

source-map-support.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ retrieveFileHandlers.push(function(path) {
8080
}
8181
} else if (fs.existsSync(path)) {
8282
// Otherwise, use the filesystem
83-
contents = fs.readFileSync(path, 'utf8');
83+
try {
84+
contents = fs.readFileSync(path, 'utf8');
85+
} catch (er) {
86+
contents = '';
87+
}
8488
}
8589

8690
return fileContentsCache[path] = contents;
@@ -388,7 +392,11 @@ function getErrorSource(error) {
388392

389393
// Support files on disk
390394
if (!contents && fs && fs.existsSync(source)) {
391-
contents = fs.readFileSync(source, 'utf8');
395+
try {
396+
contents = fs.readFileSync(source, 'utf8');
397+
} catch (er) {
398+
contents = '';
399+
}
392400
}
393401

394402
// Format the line from the original source code like node does

test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,28 @@ it('normal throw', function() {
143143
]);
144144
});
145145

146+
/* The following test duplicates some of the code in
147+
* `normal throw` but triggers file read failure.
148+
*/
149+
it('fs.readFileSync failure', function() {
150+
compareStackTrace(createMultiLineSourceMap(), [
151+
'var fs = require("fs");',
152+
'var rfs = fs.readFileSync;',
153+
'fs.readFileSync = function() {',
154+
' throw new Error("no rfs for you");',
155+
'};',
156+
'try {',
157+
' throw new Error("test");',
158+
'} finally {',
159+
' fs.readFileSync = rfs;',
160+
'}'
161+
], [
162+
'Error: test',
163+
/^ at Object\.exports\.test \((?:.*[/\\])?line7\.js:1007:107\)$/
164+
]);
165+
});
166+
167+
146168
it('throw inside function', function() {
147169
compareStackTrace(createMultiLineSourceMap(), [
148170
'function foo() {',

0 commit comments

Comments
 (0)