-
-
Notifications
You must be signed in to change notification settings - Fork 702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throw custom error (ES6) #596
Comments
Hey @theofidry thanks for the issue. That should definitely work. The way we handle errors is getting better in
|
Indeed this was using the 3.4.1 version, should have said so from the beginning. I'm going to test it with the 4.x.x branch and come back to you. |
Hey @theofidry have you had a chance to try out |
Unfortunately no, busy times 😞. But think I will be able to do it today or during the weekend... |
Great! I look forward to seeing how you go with it 😄 |
Nope doesn't work :( haircvt/serializerjs#14 |
Ha, looks like the
|
Okay. Thanks very much for trying it out. It's interesting that it says If you have the time to investigate what is happening, or perhaps making a small reduced test case. That'd be amazing! |
It's Will try to do a reduced test case with the snippet above. |
👍 awesome thanks 😄 |
I'll take another look on the @theofidry could give me more details on this?
If I'm not missing anything here, that output doesn't correspond to the example code you posted (where does that Hopefully we've got every possible combination of arguments correctly handled on #683, so if there's any error on this it may be due to the lack of polishment on the Thanks for your time @theofidry, I'll keep this one for now so we can investigate it further. |
Hi @lucasfcosta, I'll try to take a look at it again during the weekend :) |
Hello guys, I just want to update everyone on this: I already finished the polishments on the I'm working to fix it and I plan on sending a PR for that tomorrow. I may need to change the |
@lucasfcosta Was this resolved via #742? |
@meeber I think it should have been, but I cannot guarantee, I'd like to see @theofidry's example and/or wait for him to test it with his whole code before closing this one. I don't have enough information for now, since I can't see the whole code involved. |
Closing this. Can always reopen if there's still an issue. |
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: Is a message mandatory for custom errors? |
@samiraguiar The following works for me using Chai v3.5 and Node v7.5 (without TypeScript/transpilation): const message = `error message`;
class InvalidReferenceFormatError extends Error {
constructor() {
super(message);
this.name = "InvalidReferenceFormatError";
this.stack = (new Error()).stack;
}
}
it('blah', function () {
expect(() => { throw new InvalidReferenceFormatError() }).to.throw(InvalidReferenceFormatError);
}); My best guess is that your code is being transpiled to something that's incompatible with the |
You're right, @meeber, the transpiled code is to blame. Because I'm using transpilation to es5, it does some workarounds for inheritance. Here's the offending transpiled code: require("mocha");
var chai = require("chai");
// ----- generated code -----
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var message = "error message";
var InvalidReferenceFormatError = (function (_super) {
__extends(InvalidReferenceFormatError, _super);
function InvalidReferenceFormatError() {
var _this = _super.call(this, message) || this;
_this.name = "InvalidReferenceFormatError";
_this.stack = new Error().stack;
return _this;
}
return InvalidReferenceFormatError;
}(Error));
// ---------------------
it('bla', function () {
chai.expect(function () { throw new InvalidReferenceFormatError(); }).to.throw(InvalidReferenceFormatError);
}); Out of curiosity, any idea why it doesn't work in this case? |
@samiraguiar Yeah, the transpiled version is creating the error in a weird way in which the thrown error object isn't considered an var meh = new InvalidReferenceFormatError();
console.log(meh instanceof InvalidReferenceFormatError); The above prints |
Thanks @meeber, it seems that this is a known issue. I'll just have to do some workarounds to get this working. |
This is a known issue among all transpilers as ES5 environments don't support extending for builtins like Array, Error etc. The same is true for Babel (docs here). This is pretty much a dupe of #909, #773 and I think a few others. It might be worth documenting this in the
Which I still a valid and justified sentiment, but - pragmatically - we're spending more time on circling round duplicate issues that documenting this issue within our own docs might just be the less time-consuming option for this instance. |
In the breaking changes documentation for typescript 2.1 (https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work) it is suggested, to explicitly set the prototype of the custom error to allow for typechecks via instanceof: class FooError extends Error {
constructor(m: string) {
super(m);
// Set the prototype explicitly.
Object.setPrototypeOf(this, FooError.prototype);
}
sayHello() {
return "hello " + this.message;
}
} After following this, it seems |
Given a custom error:
And the following function:
The following doesn't work:
And instead is throwing:
The text was updated successfully, but these errors were encountered: