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

fix: use a more intuitive err-code #278

Closed
wants to merge 3 commits into from
Closed

Conversation

tabcat
Copy link
Contributor

@tabcat tabcat commented Oct 5, 2022

Related: libp2p/js-libp2p#1269

This PR was created in js-libp2p-crypto to dial-in/double-check what is needed before making more changes in other repos.

Uses a more intuitive err-code; the repo is currently located at coderra.

The issue OP's implementation is used. The parameters and behavior have changed compared with err-code.

src/index.ts:
// https://github.com/libp2p/js-libp2p/issues/1269#issue-1280763716
export default 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 // eslint-disable-line @typescript-eslint/consistent-type-assertions
  }
}

CodeError behavior vs. errcode:

  • string required as first param, not an Error
  • message: will not throw at runtime if given something other than a string
  • code: will not throw at runtime if given something other than a string, required to be a string by typescript/cannot sometimes be props
  • props: properties of props are not assigned to directly Error instance
  • Cannot be used to throw TypeError's

@tabcat
Copy link
Contributor Author

tabcat commented Oct 7, 2022

Going to make the repo look better and add more tests sometime in the next few days. will then mark undrafted.

Edit: have been busy with other things... will do this when I have time; maybe before or during IPFS camp.

@tabcat
Copy link
Contributor Author

tabcat commented Nov 7, 2022

Closing from lack of input, wanted to have a reference before opening a bunch of PRs. Also don't like the coderra name anymore, error-code as a package name for something like this seems much better.

@tabcat tabcat closed this Nov 7, 2022
@wemeetagain
Copy link
Member

This looks good. I'm curious about how the generic type works in practice. (This repo doesn't seem to use the "props")

Tag me when you open another PR and I'll be sure to give you a review.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants