Anthony O.
Anthony O.

Reputation: 24397

How to specify a "caused by" in a JavaScript Error?

In my NodeJS program, I parse some user JSON file.

So I use :

this.config = JSON.parse(fs.readFileSync(path));

The problem is that if the json file is not correctly formated, the error thrown is like:

undefined:55
            },
            ^
SyntaxError: Unexpected token }
    at Object.parse (native)
    at new MyApp (/path/to/docker/lib/node_modules/myApp/lib/my-app.js:30:28)
...

As it is not really user friendly I would like to throw an Error specifying some user friendly message (like "your config file is not well formated") but I want to keep the stacktrace in order to point to the problematic line.

In the Java world I used throw new Exception("My user friendly message", catchedException) in order to have the original exception which caused that one.

How is it possible in the JS world?

Upvotes: 33

Views: 12260

Answers (6)

hamza moeen
hamza moeen

Reputation: 1

try {
  throw new Error('Original error');
} catch (originalError) {
  throw new Error('New error message', { cause: originalError });
}

Upvotes: 0

Sebastien Lorber
Sebastien Lorber

Reputation: 92150

There is an new Error Cause proposal for ECMAScript, and it reached stage-4 at TC34!

It means it will be in the next ECMAScript version!

https://github.com/tc39/proposal-error-cause

You would provide the cause as an error option:

throw new Error(`Couldn't parse file at path ${filePath}`, { cause: err });

The ES proposal only formalize it on the language level, but browsers/NodeJS should normally agree to log the full causal chain in practice (see https://github.com/nodejs/node/issues/38725)


As of today (end of 2021), Firefox Devtools are already able to log nested stacktraces!

enter image description here

Upvotes: 11

Erin
Erin

Reputation: 5835

2021 Update: To chain exceptions in JS:

class MyAppError extends Error {
    constructor(...params) {
        super(...params)
        if (Error.captureStackTrace) {
            // This is the key line!
            Error.captureStackTrace(this, this.constructor);
        }
        this.name = this.constructor.name
    }
}

See the Mozilla docs on Error.captureStackTrace

Upvotes: 0

eliocs
eliocs

Reputation: 18827

Joyent released a Node.js package that can be used exactly for that. It is called VError. I paste an example of how you would use the pacakge:

var fs = require('fs');
var filename = '/nonexistent';
fs.stat(filename, function (err1) {
    var err2 = new VError(err1, 'stat "%s"', filename);
    console.error(err2.message);
});

would print the following:

stat "/nonexistent": ENOENT, stat '/nonexistent'

Upvotes: 4

Anthony O.
Anthony O.

Reputation: 24397

What I finally did is:

try {
    this.config = JSON.parse(fs.readFileSync(path));
} catch(err) {
    var newErr = new Error('Problem while reading the JSON file');
    newErr.stack += '\nCaused by: '+err.stack;
    throw newErr;
}

Upvotes: 39

Moob
Moob

Reputation: 16184

Use a try / catch block:

try {
    this.config = JSON.parse("}}junkJSON}");
    //...etc
}
catch (e) {
    //console.log(e.message);//the original error message 
    e.message = "Your config file is not well formatted.";//replace with new custom message
    console.error(e);//raise the exception in the console
    //or re-throw it without catching
    throw e;
}

http://jsfiddle.net/0ogf1jxs/5/

UPDATE: If you really feel the need for a custom error you can define your own:

function BadConfig(message) {
   this.message = message;
   this.name = "BadConfig";
}
BadConfig.prototype = new Error();
BadConfig.prototype.constructor = BadConfig;

try {
    this.config = JSON.parse("}}badJson}");
} catch(e) {
    throw new BadConfig("Your JSON is wack!");
}

http://jsfiddle.net/kL394boo/

Lots of useful info at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error

Upvotes: -4

Related Questions