chatmail / chatmail/yerpc

Should Error be `instanceof Error`?

Open
#69 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
51
Forks
9
Avg merge
21m
Merged PRs (30d)
2

Description

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: https://github.com/deltachat/deltachat-desktop/pull/5008.

I'd suggest to define a class

```typescript
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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.