Skip to content

Instantly share code, notes, and snippets.

@erikologic
Created August 12, 2017 02:41
Show Gist options
  • Select an option

  • Save erikologic/f499c4697a5f3e650f37def3ec8acc22 to your computer and use it in GitHub Desktop.

Select an option

Save erikologic/f499c4697a5f3e650f37def3ec8acc22 to your computer and use it in GitHub Desktop.

Revisions

  1. erikologic created this gist Aug 12, 2017.
    9 changes: 9 additions & 0 deletions Error.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    module.exports = function CustomError(message) {
    this.name = this.constructor.name
    this.message = message
    Error.captureStackTrace(this, this.constructor)
    }

    module.exports.prototype.inspect = function () {
    return this.stack
    }
    15 changes: 15 additions & 0 deletions Error.test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    class MyCustomError extends require('./Errors') {}

    try {
    throw new MyCustomError ('message')
    } catch (err) {
    console.log(err.constructor.name);
    console.log(err instanceof MyCustomError)
    console.log(err);
    }

    try {
    throw new MyCustomError ()
    } catch (err) {
    console.log(err);
    }
    24 changes: 24 additions & 0 deletions console.log
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    MyCustomError
    true
    MyCustomError: message
    at Object.<anonymous> (......./Errors.test.js:4:9)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)
    at bootstrap_node.js:542:3
    MyCustomError
    at Object.<anonymous> (......./Errors.test.js:13:9)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)
    at bootstrap_node.js:542:3