Skip to content

Commit

Permalink
Improve createErrorType()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jun 5, 2022
1 parent 9d99e3d commit 2702ed4
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/error/create.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Create an error type with a specific `name`
// Create an error type with a specific `name`.
// The constructor allows setting either `error.cause` or any properties:
// `new ErrorType('message', { anyProp: true })`
export const createErrorType = function (name) {
return class extends Error {
constructor(message, opts) {
super(message, opts)
constructor(message, { cause, ...props } = {}) {
super(message, { cause })
// eslint-disable-next-line fp/no-this, fp/no-mutating-assign
Object.assign(this, props)
// eslint-disable-next-line fp/no-this, fp/no-mutation
this.name = name
}
Expand Down

0 comments on commit 2702ed4

Please sign in to comment.