Skip to content

Commit

Permalink
feat(ssri): Trait.tryRun() to handle expected error
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanssen0 committed Jan 9, 2025
1 parent 52ef11b commit e4ea183
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/sweet-jokes-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-ccc/ssri": minor
---

feat(ssri): Trait.tryRun to handle expected error
29 changes: 28 additions & 1 deletion packages/ssri/src/trait.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ccc } from "@ckb-ccc/core";
import { Executor, ExecutorResponse } from "./executor.js";
import {
Executor,
ExecutorErrorDecode,
ExecutorErrorExecutionFailed,
ExecutorResponse,
} from "./executor.js";
import { getMethodPath } from "./utils.js";

/**
Expand All @@ -19,6 +24,28 @@ export class Trait {
this.executor = executor ?? undefined;
}

static async tryRun<T>(
call: Promise<ExecutorResponse<T>>,
): Promise<ExecutorResponse<T | undefined>> {
try {
return await call;
} catch (e) {
if (
e instanceof ExecutorErrorExecutionFailed ||
e instanceof ExecutorErrorDecode
) {
return ExecutorResponse.new(undefined);
}
throw e;
}
}

async tryRun<T>(
call: Promise<ExecutorResponse<T>>,
): Promise<ExecutorResponse<T | undefined>> {
return Trait.tryRun(call);
}

assertExecutor() {
if (!this.executor) {
throw new Error("SSRI executor is not set");
Expand Down

0 comments on commit e4ea183

Please sign in to comment.