js-libp2p icon indicating copy to clipboard operation
js-libp2p copied to clipboard

Use/create a more intuitive `errcode`

Open wemeetagain opened this issue 3 years ago • 0 comments

Current usage of errcode is not intuitive. example:

throw new errcode(new Error('error in foo'), ERR_FOO, { foo: bar })

Prefer an implementation like:

export class CodeError<T extends Record<string, any>> extends Error {
  code: string
  props: T

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

    this.code = code
    this.props = props ?? {} as T
  }
}

with usage then like:

throw new CodeError('error in foo', ERR_FOO, { foo: bar })

wemeetagain avatar Jun 22 '22 19:06 wemeetagain