-
-
Notifications
You must be signed in to change notification settings - Fork 699
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
Avoid error instantiation if possible on assertThrows (aka assert.throws). Closes #220 #221
Conversation
Given a constructor as function, try to get the error name from its prototype or itself before relying on the old behaviour. Closes chaijs#220
+1 |
1 similar comment
+1 |
The problem is that the current patch breaks 2 tests. It should not. Maybe we could write something like that instead to have all tests running but still making a best effort out of it : name = constructor.prototype.name || constructor.name;
if (name === 'Error' && constructor !== Error) {
name = new constructor().name;
} Wouldn't this be "better" ? This would less likely break existing tests but still prevent constructing an error as much as possible... |
Hi! This was my first pull request to an open source project, so I expected to do something wrongly 😄. @qraynaud Your proposition seems good enough for me as its principle is the same: Given an error constructor as function, try to get the error name from its prototype or itself before relying on the old behaviour. Just to note, I'm going to test it this time, and commit if everything succeed. |
Given an error constructor as function, try to get the error name from its prototype or itself before relying on the old behaviour. Closes chaijs#220 Replaces [the old pull](chaijs#221) to avoid useless commits.
Hi, everyone. The code above really screwed me up.
Imagine you are going to make a custom error class.
And then, we meet the following:
Are you creating an instance just for detecting its name? Although it seems fine for most circumstances, a slight more complicated error class will make unexpected errors:
Many custom constructors require parameter checks, and a simple constructor call with no arguments cause disasters. I really hope we could make better decisions about the name detection. |
In fact, I am writing tests for code that uses https://github.com/composed-validations/composed-validations And
I got an error related to the constructed call mentioned above:
Better ideas? |
@RobinQu it looks as though the error is coming from the composed-validations codebase, because of required arguments of the MultiValidationError constructor. MultiValidationError - despite extending from Error, does not offer the same contract, and the constructor has a chance of throwing a new error (because it is trying to map on undefined). I've raised a PR which will fix this in your tests. |
Given an error constructor as function, try to get the error name from its prototype or itself before relying on the old behaviour. Closes #220 Replaces [the old pull](chaijs/chai#221) to avoid useless commits.
Given a constructor as function, try to get the error name from its prototype or itself before relying on the old behaviour. Closes #220