*Add rpcError helper method and change all rpc error handling to the function and use error constants
This commit is contained in:
parent
a36735a356
commit
8b17fbf3d2
|
@ -3,8 +3,13 @@ import minimatch from "minimatch";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import HTTPClient from "algosdk/dist/cjs/src/client/client.js";
|
import HTTPClient from "algosdk/dist/cjs/src/client/client.js";
|
||||||
import { sprintf } from "sprintf-js";
|
import { sprintf } from "sprintf-js";
|
||||||
import { RpcMethodList } from "./index.js";
|
import { rpcError, RpcMethodList } from "./index.js";
|
||||||
import config from "../config.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")[] } = {
|
const allowedEndpoints: { [endpoint: string]: ("GET" | "POST")[] } = {
|
||||||
"/v2/teal/compile": ["POST"],
|
"/v2/teal/compile": ["POST"],
|
||||||
|
@ -21,12 +26,12 @@ export function proxyRestMethod(
|
||||||
let chainId = maybeMapChainId(chain);
|
let chainId = maybeMapChainId(chain);
|
||||||
|
|
||||||
if (!chainId) {
|
if (!chainId) {
|
||||||
throw new Error("Invalid Chain");
|
return rpcError(ERR_INVALID_CHAIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
chainId = reverseMapChainId(chainId as string);
|
chainId = reverseMapChainId(chainId as string);
|
||||||
if (!chainId || chainId !== matchChainId) {
|
if (!chainId || chainId !== matchChainId) {
|
||||||
throw new Error("Invalid Chain");
|
return rpcError(ERR_INVALID_CHAIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
let method = args.method ?? false;
|
let method = args.method ?? false;
|
||||||
|
@ -55,7 +60,7 @@ export function proxyRestMethod(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
throw new Error("Endpoint Invalid");
|
return rpcError(ERR_ENDPOINT_INVALID);
|
||||||
}
|
}
|
||||||
|
|
||||||
let apiUrl;
|
let apiUrl;
|
||||||
|
@ -79,7 +84,7 @@ export function proxyRestMethod(
|
||||||
resp = await client.post(endpoint, data, { ...fullHeaders });
|
resp = await client.post(endpoint, data, { ...fullHeaders });
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error("Method Invalid");
|
return rpcError(ERR_METHOD_INVALID);
|
||||||
}
|
}
|
||||||
|
|
||||||
const getCircularReplacer = () => {
|
const getCircularReplacer = () => {
|
||||||
|
|
|
@ -53,13 +53,13 @@ export function proxyRpcMethod(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!chainId || !chainMatch) {
|
if (!chainId || !chainMatch) {
|
||||||
throw new Error("Invalid Chain");
|
return rpcError(ERR_INVALID_CHAIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (usePocketGateway()) {
|
if (usePocketGateway()) {
|
||||||
chainId = reverseMapChainId(chainId as string);
|
chainId = reverseMapChainId(chainId as string);
|
||||||
if (!chainId) {
|
if (!chainId) {
|
||||||
throw new Error("Invalid Chain");
|
return rpcError(ERR_INVALID_CHAIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
let provider: RpcProviderMethod | boolean =
|
let provider: RpcProviderMethod | boolean =
|
||||||
|
@ -117,3 +117,11 @@ function getRpcProvider(chain: string): RpcProviderMethod {
|
||||||
|
|
||||||
return gatewayMethods.default(chain);
|
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));
|
||||||
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
//const require = createRequire(import.meta.url);
|
//const require = createRequire(import.meta.url);
|
||||||
//import { createRequire } from "module";
|
//import { createRequire } from "module";
|
||||||
|
|
||||||
import { RpcMethodList } from "./index.js";
|
import { rpcError, RpcMethodList } from "./index.js";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import rand from "random-key";
|
import rand from "random-key";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import SPVNode from "hsd/lib/node/spvnode.js";
|
import SPVNode from "hsd/lib/node/spvnode.js";
|
||||||
import config from "../config.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
|
// @ts-ignore
|
||||||
import { NodeClient } from "hs-client";
|
import { NodeClient } from "hs-client";
|
||||||
|
|
||||||
|
@ -78,10 +78,10 @@ if (!config.bool("hsd-use-external-node")) {
|
||||||
const hnsClient = new NodeClient(clientArgs);
|
const hnsClient = new NodeClient(clientArgs);
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
getnameresource: async function (args: any, context: object) {
|
getnameresource: async (args: any, context: object) => {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if ("hns" !== context.chain) {
|
if ("hns" !== context.chain) {
|
||||||
throw new Error("Invalid Chain");
|
throw rpcError(ERR_INVALID_CHAIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
let resp;
|
let resp;
|
||||||
|
@ -93,8 +93,10 @@ export default {
|
||||||
const eMessage = e.message.toLowerCase();
|
const eMessage = e.message.toLowerCase();
|
||||||
|
|
||||||
if (eType === "rpcerror" && eMessage.includes("chain is not synced")) {
|
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;
|
return resp;
|
||||||
|
|
Loading…
Reference in New Issue