Skip to content

Preserve error constructor#70

Merged
sindresorhus merged 17 commits into
sindresorhus:mainfrom
pixiebrix:error-constructor
Apr 18, 2022
Merged

Preserve error constructor#70
sindresorhus merged 17 commits into
sindresorhus:mainfrom
pixiebrix:error-constructor

Conversation

@fregante

@fregante fregante commented Apr 2, 2022

Copy link
Copy Markdown
Collaborator
Previous notes about native-only support

Partially fix for #48.

Includes and is blocked by #69

Custom constructors will need extra logic, because what happens when CustomError’s signature requires a second parameter?

  • Deserialization should catch that and use a regular Error, and/or
  • The user should pass .set(MyError, object => new MyError(object.message, object.otherProp))

But this might require further bikeshedding, so I'm leaving it out of this PR

Fixes #48

@fregante
fregante marked this pull request as ready for review April 2, 2022 07:47
Comment thread error-constructors.js
@sindresorhus

Copy link
Copy Markdown
Owner

Merge conflict

@sindresorhus

Copy link
Copy Markdown
Owner

The behavior needs to be documented.

@fregante

Copy link
Copy Markdown
Collaborator Author

The behavior has been documented.

@fregante fregante changed the title Preserve error constructor (if native) Preserve error constructor Apr 10, 2022

@fregante fregante left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be good to go. Do you think this is a breaking change?

  deserializeError({name: 'TypeError'})
- //=> [Error: 🟣] // v9
+ //=> [TypeError: 🟣] // next

Comment thread index.d.ts
@@ -1,5 +1,7 @@
import {Primitive, JsonObject} from 'type-fest';

export {default as errorConstructors} from './error-constructors.js';

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds support for custom error constructors to be added. This only supports simple errors so I mentioned this in the docs and in the types. A new issue can be created to add support for such error constructors.

Also, instead of adding error-constructors.js to the exports map, I just added another export here. It's cleaner anyway.

Comment thread index.js
return json;
};

const getErrorConstructor = name => errorConstructors.get(name) || Error;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried ?? but it fails in Node 12. If desired, Node 12 support can probably be dropped separately so it appears in the changelog

@sindresorhus
sindresorhus merged commit 49db63a into sindresorhus:main Apr 18, 2022
@sindresorhus

Copy link
Copy Markdown
Owner

https://github.com/sindresorhus/serialize-error/releases/tag/v10.0.0

@ghost

ghost commented May 31, 2022

Copy link
Copy Markdown

@sindresorhus @fregante Looks like we have a wrong type definition for this feature,

#78

Not every Error objects would have the same constructor types

@fregante

fregante commented Jun 1, 2022

Copy link
Copy Markdown
Collaborator Author

It seems that constructors like DOMExceptions don't match ErrorConstructor. While therefore the types are incorrect, it doesn't affect the final user. If you have a solution please send a PR, but otherwise I don’t think it's important here.

I think potentially the solution would be to replace ErrorConstructor with new (string, ...anything[]) => {message: string, ...anything} so it matches any constructors that accepts a string and return an ErrorLike

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error subclasses are lost when deserializing

2 participants