Add rpc error fields (#4637)

* [WIP] Add RpcError

Currently located in rpc-core at request of maintainer. However unclear as to how it should be used, because the error
needs to be thrown in rpc-provider, and depending on it causes a dependency loop.

* Move `RpcError` to `rpc-provider`

Also add tests
This commit is contained in:
Noah Corona
2022-03-15 08:20:16 +02:00
committed by GitHub
parent 1db2136a3d
commit cccd512d70
4 changed files with 176 additions and 1 deletions
+4 -1
View File
@@ -5,6 +5,8 @@ import type { JsonRpcRequest, JsonRpcResponse, JsonRpcResponseBaseError } from '
import { assert, isNumber, isString, isUndefined, stringify } from '@polkadot/util';
import RpcError from './error';
function formatErrorData (data?: string | number): string {
if (isUndefined(data)) {
return '';
@@ -68,8 +70,9 @@ export class RpcCoder {
private _checkError (error?: JsonRpcResponseBaseError): void {
if (error) {
const { code, data, message } = error;
const fmtMessage = `${code}: ${message}${formatErrorData(data)}`;
throw new Error(`${code}: ${message}${formatErrorData(data)}`);
throw new RpcError(fmtMessage, code, data);
}
}
}