diff --git a/src/rpc/algorand.ts b/src/rpc/algorand.ts index 4c59518..816e1cf 100644 --- a/src/rpc/algorand.ts +++ b/src/rpc/algorand.ts @@ -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 = () => { diff --git a/src/rpc/common.ts b/src/rpc/common.ts index e573c79..fd5f57c 100644 --- a/src/rpc/common.ts +++ b/src/rpc/common.ts @@ -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 { + return Promise.reject(new RpcError(message)); +} diff --git a/src/rpc/handshake.ts b/src/rpc/handshake.ts index 9b5b879..c2aa275 100644 --- a/src/rpc/handshake.ts +++ b/src/rpc/handshake.ts @@ -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;