-
Notifications
You must be signed in to change notification settings - Fork 9
What type of value is "cause" meant to contain? #21
Comments
Before I read this, I add assumed that cause was an Error. It would be good to get this clarified so everyone is talking about the same thing. |
Yes, that was intent in which we presented the proposal. Also here is a fiddle with that behavior. |
What, exactly, was the intent? The fiddle shows a string for the cause, instead of wrapping an error; are you saying the intent was to use strings? |
Well, should allow any value and wrap it internally to an error? Maybe we should avoid the the |
It seems like the best path forward is to:
|
From what I can gather if the intention is to allow some sort of I would like the cause to be an Error. Otherwise, such a proposal could become an anti-pattern and people would overused simple to ignore creating new errors in the system. I would like to see a more concrete example of the problem statement such as https://github.com/tc39/proposal-error-cause#why-not-custom-subclasses-of-error it is not clear to me what the issue is. |
Because I understand the intended usage to be try {
something();
} catch (e) {
throw new Error('something failed!', e);
} and there are no restrictions on the type of However, I think putting anything other than an Error instance in this field should be considered to be extremely un-idiomatic, just as throwing any value other than an Error is considered un-idiomatic. I am strongly against using this field to represent anything other than "the Error (as would be seen by For the case of web specs, it seems reasonable to synthesize a new Error object which contains other data and to put that object in the But perhaps I have a different understanding of the intent of this proposal than the champions. |
if it helps somehow (at least to me it does), This proposal reminds me Go and wrapping Errors, which, if I am not mistaken, is the intention of such a proposal, or at least is what I am hoping it is (the difference is that Go errors are not like JS, but shouldn't matter). Right? In that case, definitely see the value of the proposal, be able to keep the original context of the error, and keep adding more contexts to the chain of errors. |
It is true the value of cause can be any JavaScript value, for any JavaScript value being able to be thrown. However, I believe the value in the field of cause should be considered suitable and reasonable to be thrown. In that sense, we may avoid throwing plain JavaScript values like numbers or strings, and avoid forging a number or string for the field of cause. |
I can't tell what actual behavior you're supporting. Do you think that the |
@bakkot I'm supporting throwing with Error instances and placing Error instances in the field of cause. However, I'm not comfortable with validating the value of cause in the Error constructor and somehow throw a TypeError if the cause is not an Error object. So for the options @ljharb has listed:
Both above options sound good to me, but not |
The problem is "coercing" may also throw error :) So I feel allowing any value may be the simplest choice. |
I had assumed that I'm pro on A basic coercion could be: If value is not an instanceof Error, but is a string, then do |
To make compatibility with existing solutions like VError easier (#31) – could at least it be ensured that a |
JavaScript already allows (unfortunately?) anything to be thrown. I agree with @bakkot that the following should be a valid way to code: try {
something();
} catch (e) {
throw new Error('something failed!', e);
} Therefore the cause should be allowed to be anything as well. |
Whether or not this cause will be coerced to a real error before being stored is another question. I think it should only be coerced if the coercion method is guaranteed to succeed perfectly at all times. Things like coercing if possible and failing silently, or coercing if possible and keeping the original value if not possible, or anything else, would become just another point for confusion and complexity. For a coercion method that is guaranteed to succeed, see the |
Closing this as there isn't any open question to be answered. Feel free to re-open if needed. |
The explainer talks about chaining errors, or catching and re-throwing with more contextual information. That implies
cause
should usually be anError
instance of some sort.However, one of the proposal champions suggests using a string:
Is the intention of
cause
to contain arbitrary "detail" data of this sort? The example goes a bit further, even, making themessage
property into more of a "title", where thecause
property contains the actual error message.I ask because web specifications have sometimes wondered about how to include more detailed error codes or similar (e.g. for distinguishing different types of network errors), and so if
cause
is meant to contain general, potentially-string data about what happened, we might start using it as such in web specifications.The text was updated successfully, but these errors were encountered: