Skip to content
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

Handle custom error constructors with incompatible signatures #72

Open
fregante opened this issue May 3, 2022 · 0 comments
Open

Handle custom error constructors with incompatible signatures #72

fregante opened this issue May 3, 2022 · 0 comments

Comments

@fregante
Copy link
Contributor

fregante commented May 3, 2022

This is the native constructor:

class Error {
	constructor(message, options) {}
}

This is custom constructor that is compatible with serialize-error:

class CustomError extends Error {
	constructor(message) {
		super(message)
	}
}

This is custom constructor that is not compatible with serialize-error:

class CustomError extends Error {
	constructor(message, requiredParameter) {
		super(message)
		if (!requiredParameter) {
			throw new Error('requiredParameter missing')
		}
	}
}

Two-part solution:

  1. catch errors on creation and use the default Error instead, here:
    to_: new Error(),
  2. add support for custom constructors, e.g.
    errorConstructors.set('CustomError', message => new CustomError(message, {}))
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

No branches or pull requests

1 participant