Permalink
Browse files

add filename to require() json errors

otherwise it can be quite difficult to figure
out which file is busted, in our case during a build phase
  • Loading branch information...
1 parent 2d0011f commit d2912c5be6fcb0baae215b0c7b018301469ea1aa @tj committed Jun 28, 2012
Showing with 20 additions and 1 deletion.
  1. +6 −1 lib/module.js
  2. +5 −0 test/fixtures/invalid.json
  3. +9 −0 test/simple/test-require-json.js
View
@@ -471,7 +471,12 @@ Module._extensions['.js'] = function(module, filename) {
// Native extension for .json
Module._extensions['.json'] = function(module, filename) {
var content = NativeModule.require('fs').readFileSync(filename, 'utf8');
- module.exports = JSON.parse(stripBOM(content));
+ try {
+ module.exports = JSON.parse(stripBOM(content));
+ } catch (err) {
+ err.message = filename + ': ' + err.message;
+ throw err;
+ }
};
@@ -0,0 +1,5 @@
+{
+ "name": "foo",
+ "version": "0.0.1"
+ "description": "im broken"
+}
@@ -0,0 +1,9 @@
+
+var assert = require('assert');
+
+try {
+ require('../fixtures/invalid.json');
+} catch (err) {
+ var i = err.message.indexOf('test/fixtures/invalid.json: Unexpected string')
+ assert(-1 != i, 'require() json error should include path');
+}

0 comments on commit d2912c5

Please sign in to comment.