Skip to content

Commit

Permalink
feat(core): typed errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanssen0 committed Sep 1, 2024
1 parent 8629449 commit 63606db
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-walls-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-ccc/core": patch
---

feat(coree): typed errors
57 changes: 54 additions & 3 deletions packages/core/src/client/clientTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Cell, Epoch, Script, Transaction } from "../ckb/index.js";
import { Hex, hexFrom } from "../hex/index.js";
import { Num, NumLike } from "../num/index.js";
import {
Cell,
Epoch,
OutPoint,
OutPointLike,
Script,
Transaction,
} from "../ckb/index.js";
import { Hex, HexLike, hexFrom } from "../hex/index.js";
import { Num, NumLike, numFrom } from "../num/index.js";
import { apply } from "../utils/index.js";
import {
ClientCollectableSearchKeyFilterLike,
Expand Down Expand Up @@ -231,3 +238,47 @@ export type ClientBlock = {
transactions: Transaction[];
uncles: ClientBlockUncle[];
};

export interface ErrorClientBaseLike {
message: string;
code: number;
data: string;
}
export class ErrorClientBase extends Error {
public readonly message: string;
public readonly code: number;
public readonly data: string;

constructor(origin: ErrorClientBaseLike) {
super(origin.message);
this.message = origin.message;
this.code = origin.code;
this.data = origin.data;
}
}

export class ErrorClientResolveUnknown extends ErrorClientBase {
public readonly outPoint: OutPoint;
constructor(origin: ErrorClientBaseLike, outPointLike: OutPointLike) {
super(origin);
this.outPoint = OutPoint.from(outPointLike);
}
}

export class ErrorClientVerification extends ErrorClientBase {
public readonly sourceIndex: Num;
public readonly scriptCodeHash: Hex;

constructor(
origin: ErrorClientBaseLike,
public readonly source: "lock" | "inputType" | "outputType",
sourceIndex: NumLike,
public readonly errorCode: number,
public readonly scriptHashType: "data" | "type",
scriptCodeHash: HexLike,
) {
super(origin);
this.sourceIndex = numFrom(sourceIndex);
this.scriptCodeHash = hexFrom(scriptCodeHash);
}
}
46 changes: 44 additions & 2 deletions packages/core/src/client/jsonRpc/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TransactionLike } from "../../ckb/index.js";
import { OutPoint, TransactionLike } from "../../ckb/index.js";
import { Hex, HexLike, hexFrom } from "../../hex/index.js";
import { Num, NumLike, numFrom, numToHex } from "../../num/index.js";
import { apply } from "../../utils/index.js";
Expand All @@ -8,6 +8,10 @@ import {
ClientFindCellsResponse,
ClientIndexerSearchKeyLike,
ClientTransactionResponse,
ErrorClientBase,
ErrorClientBaseLike,
ErrorClientResolveUnknown,
ErrorClientVerification,
OutputsValidator,
} from "../clientTypes.js";
import {
Expand Down Expand Up @@ -289,7 +293,45 @@ export abstract class ClientJsonRpc extends Client {
),
);

return transform(await this.send(payload), outTransformer);
try {
return transform(await this.send(payload), outTransformer);
} catch (errAny: unknown) {
if (typeof errAny !== "object" || errAny === null) {
throw errAny;
}
const err = errAny as ErrorClientBaseLike;

const unknownOutPointMatch = err.data.match(
new RegExp("Resolve\\(Unknown\\(OutPoint\\((0x.*)\\)\\)\\)"),
)?.[1];
if (unknownOutPointMatch) {
throw new ErrorClientResolveUnknown(
err,
OutPoint.fromBytes(unknownOutPointMatch),
);
}
const verificationFailedMatch = err.data.match(
new RegExp(
"Verification\\(Error { kind: Script, inner: TransactionScriptError { source: (Inputs|Outputs)\\[([0-9]*)\\].(Lock|Type), cause: ValidationFailure: see error code (-?[0-9])* on page https://nervosnetwork\\.github\\.io/ckb-script-error-codes/by-(type|data)-hash/(.*)\\.html",
),
);
if (verificationFailedMatch) {
throw new ErrorClientVerification(
err,
verificationFailedMatch[3] === "Lock"
? "lock"
: verificationFailedMatch[1] === "Inputs"
? "inputType"
: "outputType",
verificationFailedMatch[2],
Number(verificationFailedMatch[4]),
verificationFailedMatch[5] === "data" ? "data" : "type",
verificationFailedMatch[6],
);
}

throw new ErrorClientBase(err);
}
};
}

Expand Down

0 comments on commit 63606db

Please sign in to comment.