digidem / comapeo-core

A local-first library for collaborating on mapping projects
MIT License
8 stars 1 forks source link

Custom / consistent errors #84

Open gmaclennan opened 1 year ago

gmaclennan commented 1 year ago

I think we need to define the errors we raise with consistent error codes. This will enable the front end to display the correct UI to the user depending on the type of error.

Some errors will be expected, e.g.

The simple way to do this is with instance checks e.g.

class ErrNotFound extends Error {
  constructor(message) {
    super(message)
  }
}

if (err instanceof ErrNotFound) {
  //...
}

However this doesn't work over RPC. I recommend defining error codes, then each error should have a .code property. rpc-reflector uses serialize-error to serialize errors over RPC, so it will preserve error.code properties.

I was just looking through the Node code for how they define errors, which seems like a good start. I would have thought there would be a neat library on npm to define errors, but it's a hard thing to find the right search terms.

sethvincent commented 1 year ago

:+1:

I've used this one before and it worked out: https://github.com/mafintosh/custom-error-class

I feel like there was another that was interesting but can't find it. I'll update if I do.