File tree Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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+ / ^ a t O b j e c t \. e x p o r t s \. t e s t \( (?: .* [ / \\ ] ) ? l i n e 7 \. j s : 1 0 0 7 : 1 0 7 \) $ /
164+ ] ) ;
165+ } ) ;
166+
167+
146168it ( 'throw inside function' , function ( ) {
147169 compareStackTrace ( createMultiLineSourceMap ( ) , [
148170 'function foo() {' ,
You can’t perform that action at this time.
0 commit comments