Throw custom error (ES6)
See original GitHub issueGiven 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:
- Created 8 years ago
- Reactions:1
- Comments:24 (13 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @meeber, it seems that this is a known issue. I’ll just have to do some workarounds to get this working.
I think I might have a similar issue.
I have the following custom error (Typescript code below):
And the assertion:
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.