Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

refactor: handling godwoken error #512

Merged
merged 6 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions packages/api-server/src/cache/data.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { handleGwError } from "../methods/gw-error";

require("newrelic");
import { createClient } from "redis";
import { envConfig } from "../base/env-config";
Expand Down Expand Up @@ -178,7 +180,7 @@ export class RedisDataCache {
);
}
await releaseLock(lockValue);
throw new Error(error.message);
throw error;
}
}

Expand All @@ -199,7 +201,7 @@ export class RedisDataCache {
`[${this.constructor.name}]: subscribe err:`,
error.message
);
throw new Error(error.message);
throw error;
}
}

Expand Down Expand Up @@ -281,12 +283,15 @@ export function errorResult(reason: string) {
}

export function parseExecuteResult(res: string) {
const jsonRes: PublishExecuteResult = JSON.parse(res);
if (jsonRes.status === PublishExecuteResultStatus.Success) {
return jsonRes.data!;
const executionResult = JSON.parse(res);
if (executionResult?.status === PublishExecuteResultStatus.Success) {
return (executionResult as PublishExecuteResult).data;
}
if (executionResult?.err != null) {
handleGwError(executionResult.err);
}

throw new Error("[RedisSubscribeResult]" + jsonRes.err);
throw new Error("[RedisSubscribeResult] unrecognizable result: " + res);
}

const asyncSleep = async (ms = 0) => {
Expand Down
1 change: 1 addition & 0 deletions packages/api-server/src/methods/error-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const RESOURCE_UNAVAILABLE = -32002;
export const TRANSACTION_REJECTED = -32003;
export const METHOD_NOT_SUPPORT = -32004;
export const LIMIT_EXCEEDED = -32005;
export const TRANSACTION_EXECUTION_ERROR = -32000;

// Polyjuice Chain custom error
// TODO - WEB3_ERROR is pretty generalize error
Expand Down
16 changes: 14 additions & 2 deletions packages/api-server/src/methods/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ import { JSONRPCError } from "jayson";
import { logger } from "../base/logger";
import { HEADER_NOT_FOUND_ERR_MESSAGE } from "./constant";
import {
TRANSACTION_EXECUTION_ERROR,
HEADER_NOT_FOUND_ERROR,
INTERNAL_ERROR,
INVALID_PARAMS,
LIMIT_EXCEEDED,
METHOD_NOT_SUPPORT,
WEB3_ERROR,
} from "./error-code";
import { HexString } from "@ckb-lumos/base";

export class RpcError extends Error implements JSONRPCError {
code: number;
data?: object;
data?: any;

constructor(code: number, message: string, data?: object) {
constructor(code: number, message: string, data?: any) {
super(message);
this.name = "RpcError";

Expand All @@ -23,6 +25,16 @@ export class RpcError extends Error implements JSONRPCError {
}
}

export function isRpcError(err: any): err is RpcError {
return err.name === "RpcError";
}

export class TransactionExecutionError extends RpcError {
constructor(message: string, data?: HexString) {
super(TRANSACTION_EXECUTION_ERROR, message, data);
}
}

export class Web3Error extends RpcError {
constructor(message: string, data?: object) {
super(WEB3_ERROR, message, data);
Expand Down
Loading