-
-
Notifications
You must be signed in to change notification settings - Fork 7
Open
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: 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
Labels
No labels