HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavascriptMinor

An object for passing a chain of errors in JavaScript

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javascriptpassingforerrorsobjectchain

Problem

I want to pass errors in errors in order to know exactly the chain of causes, but without augmenting or even reading any stack until this becomes really necessary. And this is how I would achieve this:

class RError extends Error {
    constructor(options = {}) {
        super();
        this.name = options.name;
        this.message = options.message;
        this.cause = options.cause;
    }
}


Example usage:

try {
    throw new RError({
        name: 'FOO',
        message: 'Something went wrong.',
        cause: new RError({
            name: 'BAR',
            message: 'I messed up.'
        })
    });
} catch (err) {
    console.error(err);
}


Here is the result:

{ FOO: Something went wrong.
    at RError (/Users/boris/Workspace/playground/index.js:5:9)
    at Object. (/Users/boris/Workspace/playground/index.js:13:11)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:456:3 name: 'FOO', message: 'Something went wrong.', cause:
   { BAR: I messed up.
       at RError (/Users/boris/Workspace/playground/index.js:5:9)
       at Object. (/Users/boris/Workspace/playground/index.js:16:16)
       at Module._compile (module.js:541:32)
       at Object.Module._extensions..js (module.js:550:10)
       at Module.load (module.js:458:32)
       at tryModuleLoad (module.js:417:12)
       at Function.Module._load (module.js:409:3)
       at Function.Module.runMain (module.js:575:10)
       at startup (node.js:160:18)
       at node.js:456:3 name: 'BAR', message: 'I messed up.', cause: undefined } }


Since I couldn’t find anyone on the web who uses this technique, I’m wondering if there is any drawback to this approach.

Update: I’ve created two fiddles, an es5 and an es6 version (run, s

Solution

Cosmetically speaking, you can use the ES6 destructuring operator to make your arguments a bit more readable:

class RError extends Error {
    constructor({name, message, cause}) {
        super(name, message);
        this.cause = cause;
    }
}


As for drawbacks, the only one I can think of is having to do it yourself instead of having the language do it for you (Like, in example, in Java, which is where I guess this is coming from). So you'll need to remember to do it every time you have a rethrown exception. Personally, I wouldn't find this acceptable, as I would surely forget.

Code Snippets

class RError extends Error {
    constructor({name, message, cause}) {
        super(name, message);
        this.cause = cause;
    }
}

Context

StackExchange Code Review Q#144441, answer score: 2

Revisions (0)

No revisions yet.