Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
feat: add CodeError (#314)
Browse files Browse the repository at this point in the history
Adds [CodeError](libp2p/js-libp2p#1269) to errors.ts

Related: libp2p/js-libp2p-crypto#284 (comment)

Added public to props and code to match AbortCode style.

Also recently added the custom [Error.name](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name) property to [code-err](https://github.com/tabcat/code-err) implementation.

Co-authored-by: Alex Potsides <[email protected]>
  • Loading branch information
tabcat and achingbrain authored Jan 3, 2023
1 parent 87b4608 commit 59aa8f7
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/interfaces/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,18 @@ export class AbortError extends Error {
return 'aborted'
}
}

export class CodeError<T extends Record<string, any> = Record<string, never>> extends Error {
public readonly props: T

constructor (
message: string,
public readonly code: string,
props?: T
) {
super(message)

this.name = props?.name ?? 'CodeError'
this.props = props ?? {} as T // eslint-disable-line @typescript-eslint/consistent-type-assertions
}
}

0 comments on commit 59aa8f7

Please sign in to comment.