From 59aa8f7fd2a8e80fc28e76fef320c531884e7e00 Mon Sep 17 00:00:00 2001 From: tabcat Date: Tue, 3 Jan 2023 11:19:24 -0600 Subject: [PATCH] feat: add CodeError (#314) Adds [CodeError](https://github.com/libp2p/js-libp2p/issues/1269) to errors.ts Related: https://github.com/libp2p/js-libp2p-crypto/pull/284#issuecomment-1324005434 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 --- packages/interfaces/src/errors.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/interfaces/src/errors.ts b/packages/interfaces/src/errors.ts index 8f6987378..b5f59d863 100644 --- a/packages/interfaces/src/errors.ts +++ b/packages/interfaces/src/errors.ts @@ -22,3 +22,18 @@ export class AbortError extends Error { return 'aborted' } } + +export class CodeError = Record> 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 + } +}