From 820248641bb2b2216ad7faf09db61e0567e5bcfa Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Thu, 11 Jan 2024 11:56:17 +0100 Subject: [PATCH] better error handling --- packages/data-layer/src/allo/backends/allo-v1.ts | 4 ++-- packages/data-layer/src/allo/common.ts | 4 +++- packages/data-layer/src/allo/index.ts | 13 ++++++++++++- packages/data-layer/src/allo/transaction-sender.ts | 6 ++++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/packages/data-layer/src/allo/backends/allo-v1.ts b/packages/data-layer/src/allo/backends/allo-v1.ts index bc1c698332..c1c27c457f 100644 --- a/packages/data-layer/src/allo/backends/allo-v1.ts +++ b/packages/data-layer/src/allo/backends/allo-v1.ts @@ -1,5 +1,5 @@ import { Address, Hex } from "viem"; -import { Allo, AlloOperation } from "../../allo"; +import { Allo, AlloError, AlloOperation } from "../index"; import { TransactionReceipt, TransactionSender, @@ -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); } diff --git a/packages/data-layer/src/allo/common.ts b/packages/data-layer/src/allo/common.ts index 24e6c58900..c2b08cdc93 100644 --- a/packages/data-layer/src/allo/common.ts +++ b/packages/data-layer/src/allo/common.ts @@ -1,3 +1,5 @@ +import { AlloError } from "."; + export type Result = | { type: "success"; value: T } | { type: "error"; error: Error }; @@ -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)); } } diff --git a/packages/data-layer/src/allo/index.ts b/packages/data-layer/src/allo/index.ts index cf094ccf11..fdc705052e 100644 --- a/packages/data-layer/src/allo/index.ts +++ b/packages/data-layer/src/allo/index.ts @@ -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. @@ -13,7 +24,7 @@ export { AlloOperation }; * * @example * ```typescript - * const allo = new AlloV1({}); + * const allo = new AlloV1({ .. }); * * const result = await allo * .createProject({ diff --git a/packages/data-layer/src/allo/transaction-sender.ts b/packages/data-layer/src/allo/transaction-sender.ts index ac2990f0b1..2f50fd0ce9 100644 --- a/packages/data-layer/src/allo/transaction-sender.ts +++ b/packages/data-layer/src/allo/transaction-sender.ts @@ -6,6 +6,7 @@ import { encodeFunctionData, } from "viem"; import { Result, error, success } from "./common"; +import { AlloError } from "."; export interface TransactionData { to: Hex; @@ -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), + ); } }