-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Structured cloning of Error .name #5665
Comments
I think this is reasonable. If you subclass the other structured-cloneable types, e.g. |
What are you referring to ?
Indeed. That also had being somewhat painful with regard to nodejs
I think you meant back to base |
The current behavior.
Correct. |
@domenic do you think supporting custom error identifiers (other than with a message field) is a reasonable request worth having discussion about ? I agree with your point that current behavior aligns with the behavior in regards to other types. At the same time it is unclear why changes to const main = async () => {
const error = await structuredClone(Object.assign(new Error('original'), { message: 'changed', name: 'OtherError' }))
error.message // => "changed"
error.name // => "Error"
} I imagine this had been debated, but I'm unable to find it. |
I'd suggest authors package up custom data they want to transfer alongside their Otherwise, I think it would be neat to improve structured cloning and make it extensible, but that's hard problem. You can see some previous attempts at https://github.com/littledan/serializable-objects |
Does not that defeat the the purpose of having #4268 ? Here is the slightly modified example you've provided there: // page.js
const ts = new TransformStream();
ts.writable.write(1);
ts.writable.write(2);
ts.writable.abort(new TimeoutError("Oh no"));
const worker = new Worker("worker.js"); Doing abort
That does seem like a hard problem, but just copying |
Since we cannot preserve subclasses across realms (unless the custom serializability proposal happens), the example you quote would not work as expected even if we did preserve the name. In particular, causing |
It depends what user expects there. If user expects that
In that regard if use expectations is
I am sorry but I do not get that argument. I would buy it if |
Just to clarify originally I was proposing that derived error types were treated differently, but since I have recognized @domenic's argument for not doing that. Since I have attempted to shift discussion towards just treating
|
Structured cloning of native error types (as per #4268) normalizes
.name
of theError
back toError
unless it's one of the standard error names. This seems to lead to unexpected behavior with derived Error types. For illustration please consider following example:.stack
seems to have capturer name at error site, while.toString()
reflects a new name. (Although I suppose that might be implementation bug in how chrome copies stack instead of expected behavior).name
end up asError
even though it was a custom Error class with a different name. (This lead to a bug in our code where we fallback to custom serialization ifDataCloneError
is thrown)I would like to point out that identifying custom errors by their
.name
(or custom.code
property established by nodejs) is fairly common. Which is why it is very unfortunate that neither works with structured cloning algorithm. Which is why I would like to propose to either:.name
on error types (or at least on derived error types) which better support common use case.The text was updated successfully, but these errors were encountered: