Skip to content

Should Error be instanceof Error? #69

@WofWca

Description

@WofWca

Catching RPC errors in TypeScript is a little hard. Usually you'd want to use instanceof Error to access its message property. We had an issue with this in Delta Chat: deltachat/deltachat-desktop#5008.

I'd suggest to define a class

class JSONRPCError extends Error {
  constructor(errObj) {
    super(errObj.message);
    this.code = errObj.code;
    this.data = errObj.data;
  }
}

However, this might be breaking because

JSON.stringify(new JSONRPCError({ code: -1, data: { a: 1 }, message: 'some error' }))
  === '{"code":-1,"data":{"a":1}}'

, i.e. message is missing. OTOH

console.log(`${new JSONRPCError({ code: -1, data: { a: 1 }, message: 'some error' })}`)

prints Error: some error, i.e. the other properties are missing.

For reference, here is how others do it: https://github.com/open-rpc/client-js/pull/234/files

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions