Skip to content

Commit

Permalink
fix(ssri): error types
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanssen0 committed Jan 9, 2025
1 parent a48cccf commit 092c0a9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-rivers-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-ccc/ssri": patch
---

refactor(ssri): error types
37 changes: 28 additions & 9 deletions packages/ssri/src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ export type ContextScript = {
tx?: undefined | null;
};

export class ExecutorErrorUnknown extends Error {
constructor(msg?: string) {
super(msg);
}
}

export class ExecutorErrorExecutionFailed extends Error {
constructor(msg?: string) {
super(msg);
}
}

export class ExecutorErrorDecode extends Error {
constructor(msg?: string) {
super(msg);
}
}

export type ContextCode =
| undefined
| {
Expand All @@ -39,13 +57,11 @@ export class ExecutorResponse<T> {
}

map<U>(fn: (res: T) => U): ExecutorResponse<U> {
return new ExecutorResponse(fn(this.res), this.cellDeps);
}
}

export class ExecutorErrorExecutionFailed extends Error {
constructor(msg?: string) {
super(msg);
try {
return new ExecutorResponse(fn(this.res), this.cellDeps);
} catch (err) {
throw new ExecutorErrorDecode(JSON.stringify(err));
}
}
}

Expand Down Expand Up @@ -99,7 +115,7 @@ export class ExecutorJsonRpc extends Executor {
!("code" in errAny) ||
typeof errAny.code !== "number"
) {
throw errAny;
throw new ExecutorErrorUnknown(JSON.stringify(errAny));
}

if (errAny.code === 1003 || errAny.code === 1004) {
Expand All @@ -109,7 +125,10 @@ export class ExecutorJsonRpc extends Executor {
throw new ExecutorErrorExecutionFailed();
}

throw errAny;
if ("message" in errAny && typeof errAny.message === "string") {
throw new ExecutorErrorUnknown(errAny.message);
}
throw new ExecutorErrorUnknown();
});
}

Expand Down

0 comments on commit 092c0a9

Please sign in to comment.