question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Throw custom error (ES6)

See original GitHub issue

Given a custom error:

export default class DummyError extends Error {
    /**
     * @param {string} [message='']
     */
    constructor(message = '') {
        super(message);

        this.name = 'DummyError';
        this.message = message;

        if (Error.hasOwnProperty('captureStackTrace')) {
            Error.captureStackTrace(this, this.constructor);
        }

        this.stack = (new Error(message)).stack;
    }
}

And the following function:

function throwDummyError() {
    throw new DummyError();
}

The following doesn’t work:

assert.throw(throwDummyError, DummyError);

And instead is throwing:

AssertionError: expected [Function: DummyError] to throw 'DummyError' but 'my error message' was thrown

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:24 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
samiraguiarcommented, Feb 20, 2017

Thanks @meeber, it seems that this is a known issue. I’ll just have to do some workarounds to get this working.

1reaction
samiraguiarcommented, Feb 18, 2017

I think I might have a similar issue.

I have the following custom error (Typescript code below):

const message: string = `error message`;

export class InvalidReferenceFormatError extends Error {
    constructor() {
        super(message);
        this.name = "InvalidReferenceFormatError";
        this.stack = (<any> new Error()).stack;
    }
}

And the assertion:

expect(() => { throw new InvalidReferenceFormatError() }).to.throw(InvalidReferenceFormatError);

This results in: AssertionError: expected [Function] to throw 'InvalidReferenceFormatError' but 'InvalidReferenceFormatError: error message' was thrown

Is a message mandatory for custom errors? Using chai version 3.5.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom JavaScript Errors in ES6 - Jamund Ferguson
Error objects are the only way to get stack traces in JavaScript. If you callback with or throw or reject a promise with...
Read more >
Extending Error in Javascript with ES6 syntax & Babel
This is important as one of the core ideas of a language that can throw an exception is to allow custom Errors. This...
Read more >
Error - JavaScript - MDN Web Docs - Mozilla
Custom error types. You might want to define your own error types deriving from Error to be able to throw new MyError() and...
Read more >
Custom errors, extending Error - The Modern JavaScript Tutorial
JavaScript allows to use throw with any argument, so technically our custom error classes don't need to inherit from Error .
Read more >
How to Throw Exceptions in JavaScript - Rollbar
With this in mind, there are two ways to throw an exception: directly via an Error object, and through a custom object. Generic...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found