Optionally preserve current stack
Opened this issue · 4 comments
An issue I found when throwing deserialized errors is that the thrown error has no reference to this throw
point.
I think the "correct" way to throw serialized errors would be something like:
throw new Error(serialized.message, {
cause: deserializeError(serialized),
});
Two drawbacks:
new Error
doesn't benefit from #70- this isn't necessarily what people want or expect by default
So how about:
throw deserializeError(serialized, {preserveStack: true});
I'm ok with adding this, but I think the option is ambiguous. I'm asking myself, which stack is it preserving.
I think it's going to be hard to encapsulate the entire behavior and raison d'être in a property name, especially since the main verb here is "deserialize error" rather than "create error"
Names:
asCause
(doesn't explain why)captureCurrentStack
(doesn't say a new error is created)newError
(technicallydeserialize
is already creating anew Error()
)wrapped
(good description of the result but says nothing about the behavior)
I think asCause
gives the clearest picture of the result, assuming you're aware of Error#cause
, even if it doesn't explain the point of doing so.
I think asCause gives the clearest picture of the result, assuming you're aware of Error#cause, even if it doesn't explain the point of doing so.
I assumed you would just want the stack
property replaced on the deserialized error with the current stack. It now sounds like you want the option to wrap it in a new error and add the deserialized one as cause
?
Correct, check my new Error
example. I want exactly that, but safer, with less code and with the ability to use the right error constructor automatically.