Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
boudra committed Jan 11, 2024
1 parent a8d9c2a commit 8202486
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/data-layer/src/allo/backends/allo-v1.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, Hex } from "viem";
import { Allo, AlloOperation } from "../../allo";
import { Allo, AlloError, AlloOperation } from "../index";
import {
TransactionReceipt,
TransactionSender,
Expand Down Expand Up @@ -64,7 +64,7 @@ export class AlloV1 implements Allo {
emit("transactionStatus", success(receipt));
blockNumber = receipt.blockNumber;
} catch (err) {
const result = new Error("Failed to create project");
const result = new AlloError("Failed to create project");
emit("transactionStatus", error(result));
return error(result);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/data-layer/src/allo/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AlloError } from ".";

export type Result<T> =
| { type: "success"; value: T }
| { type: "error"; error: Error };
Expand All @@ -18,6 +20,6 @@ export async function uploadToIPFS(

return success(metadataCid);
} catch (err) {
return error(new Error("Failed to upload to IPFS"));
return error(new AlloError("Failed to upload to IPFS", err));
}
}
13 changes: 12 additions & 1 deletion packages/data-layer/src/allo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import { TransactionReceipt } from "./transaction-sender";

export { AlloOperation };

export class AlloError extends Error {
constructor(
message: string,
public inner: unknown = undefined,
) {
super(message);

this.name = "AlloError";
}
}

/**
* Represents the common interface for interacting with Allo contracts.
* This interface provides methods to perform various operations related to Allo contracts.
Expand All @@ -13,7 +24,7 @@ export { AlloOperation };
*
* @example
* ```typescript
* const allo = new AlloV1({});
* const allo = new AlloV1({ .. });
*
* const result = await allo
* .createProject({
Expand Down
6 changes: 4 additions & 2 deletions packages/data-layer/src/allo/transaction-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
encodeFunctionData,
} from "viem";
import { Result, error, success } from "./common";
import { AlloError } from ".";

export interface TransactionData {
to: Hex;
Expand Down Expand Up @@ -72,7 +73,8 @@ export async function sendTransaction(

return success(tx);
} catch (err) {
// TODO: define custom error with more details for better debugging
return error(new Error("Failed to send transaction .."));
return error(
new AlloError(`Failed to send transaction: ${String(err)}`, err),
);
}
}

0 comments on commit 8202486

Please sign in to comment.