*Add rpcError helper method and change all rpc error handling to the function and use error constants

This commit is contained in:
Derrick Hammer 2022-08-04 23:38:30 -04:00
parent a36735a356
commit 8b17fbf3d2
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 27 additions and 12 deletions

View File

@ -3,8 +3,13 @@ import minimatch from "minimatch";
// @ts-ignore
import HTTPClient from "algosdk/dist/cjs/src/client/client.js";
import { sprintf } from "sprintf-js";
import { RpcMethodList } from "./index.js";
import { rpcError, RpcMethodList } from "./index.js";
import config from "../config.js";
import {
ERR_ENDPOINT_INVALID,
ERR_INVALID_CHAIN,
ERR_METHOD_INVALID,
} from "../error.js";
const allowedEndpoints: { [endpoint: string]: ("GET" | "POST")[] } = {
"/v2/teal/compile": ["POST"],
@ -21,12 +26,12 @@ export function proxyRestMethod(
let chainId = maybeMapChainId(chain);
if (!chainId) {
throw new Error("Invalid Chain");
return rpcError(ERR_INVALID_CHAIN);
}
chainId = reverseMapChainId(chainId as string);
if (!chainId || chainId !== matchChainId) {
throw new Error("Invalid Chain");
return rpcError(ERR_INVALID_CHAIN);
}
let method = args.method ?? false;
@ -55,7 +60,7 @@ export function proxyRestMethod(
}
if (!found) {
throw new Error("Endpoint Invalid");
return rpcError(ERR_ENDPOINT_INVALID);
}
let apiUrl;
@ -79,7 +84,7 @@ export function proxyRestMethod(
resp = await client.post(endpoint, data, { ...fullHeaders });
break;
default:
throw new Error("Method Invalid");
return rpcError(ERR_METHOD_INVALID);
}
const getCircularReplacer = () => {

View File

@ -53,13 +53,13 @@ export function proxyRpcMethod(
}
if (!chainId || !chainMatch) {
throw new Error("Invalid Chain");
return rpcError(ERR_INVALID_CHAIN);
}
if (usePocketGateway()) {
chainId = reverseMapChainId(chainId as string);
if (!chainId) {
throw new Error("Invalid Chain");
return rpcError(ERR_INVALID_CHAIN);
}
let provider: RpcProviderMethod | boolean =
@ -117,3 +117,11 @@ function getRpcProvider(chain: string): RpcProviderMethod {
return gatewayMethods.default(chain);
}
class RpcError extends Error {
public code: number = -1;
}
export function rpcError(message: string): Promise<RpcError> {
return Promise.reject(new RpcError(message));
}

View File

@ -1,13 +1,13 @@
//const require = createRequire(import.meta.url);
//import { createRequire } from "module";
import { RpcMethodList } from "./index.js";
import { rpcError, RpcMethodList } from "./index.js";
// @ts-ignore
import rand from "random-key";
// @ts-ignore
import SPVNode from "hsd/lib/node/spvnode.js";
import config from "../config.js";
import { ERR_NOT_READY } from "../error.js";
import { ERR_INVALID_CHAIN, ERR_NOT_READY } from "../error.js";
// @ts-ignore
import { NodeClient } from "hs-client";
@ -78,10 +78,10 @@ if (!config.bool("hsd-use-external-node")) {
const hnsClient = new NodeClient(clientArgs);
export default {
getnameresource: async function (args: any, context: object) {
getnameresource: async (args: any, context: object) => {
// @ts-ignore
if ("hns" !== context.chain) {
throw new Error("Invalid Chain");
throw rpcError(ERR_INVALID_CHAIN);
}
let resp;
@ -93,8 +93,10 @@ export default {
const eMessage = e.message.toLowerCase();
if (eType === "rpcerror" && eMessage.includes("chain is not synced")) {
return Promise.reject({ ...e, message: ERR_NOT_READY });
return rpcError(ERR_NOT_READY);
}
return rpcError(eMessage);
}
return resp;