From 6ce58a04f218153ff2c6f02cf411fccfcc366553 Mon Sep 17 00:00:00 2001 From: Andrew Min Date: Mon, 9 Sep 2024 17:34:12 -0400 Subject: [PATCH] improved error handling --- examples/with-viem/src/retry.ts | 7 +- packages/viem/src/index.ts | 118 +++++++++++++------------------- 2 files changed, 49 insertions(+), 76 deletions(-) diff --git a/examples/with-viem/src/retry.ts b/examples/with-viem/src/retry.ts index a46d9c3b3..202a5631f 100644 --- a/examples/with-viem/src/retry.ts +++ b/examples/with-viem/src/retry.ts @@ -68,12 +68,7 @@ async function main() { try { txHash = await client.sendTransaction(transactionRequest); } catch (error: any) { - console.log("error", error); - const parsedErrorMessage = JSON.parse(error["shortMessage"])["cause"][ - "shortMessage" - ]; - const parsedTurnkeyError = JSON.parse(parsedErrorMessage); - + const parsedTurnkeyError = JSON.parse(error["cause"]["details"]); const activityId = parsedTurnkeyError["activityId"]; let activityStatus = parsedTurnkeyError["activityStatus"]; diff --git a/packages/viem/src/index.ts b/packages/viem/src/index.ts index 062b1e8ea..de399f7c0 100644 --- a/packages/viem/src/index.ts +++ b/packages/viem/src/index.ts @@ -272,23 +272,19 @@ export async function getSignatureFromActivity( "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2", ].includes(activity.type) ) { - throw new BaseError( - JSON.stringify({ - message: `Unexpected activity type: ${activity.type}`, - activityId: activity.id, - activityStatus: activity.status as TActivityStatus, - }) - ); + throw new TurnkeyActivityError({ + message: `Unexpected activity type: ${activity.type}`, + activityId: activity.id, + activityStatus: activity.status as TActivityStatus, + }); } if (activity.status !== "ACTIVITY_STATUS_COMPLETED") { - throw new BaseError( - JSON.stringify({ - message: `Activity is not yet completed: ${activity.status}`, - activityId: activity.id, - activityStatus: activity.status as TActivityStatus, - }) - ); + throw new TurnkeyActivityError({ + message: `Activity is not yet completed: ${activity.status}`, + activityId: activity.id, + activityStatus: activity.status as TActivityStatus, + }); } const signature = activity.result?.signRawPayloadResult!; @@ -319,23 +315,19 @@ export async function getSignedTransactionFromActivity( "ACTIVITY_TYPE_SIGN_TRANSACTION_V2", ].includes(activity.type) ) { - throw new BaseError( - JSON.stringify({ - message: `Unexpected activity type: ${activity.type}`, - activityId: activity.id, - activityStatus: activity.status as TActivityStatus, - }) - ); + throw new TurnkeyActivityError({ + message: `Unexpected activity type: ${activity.type}`, + activityId: activity.id, + activityStatus: activity.status as TActivityStatus, + }); } if (activity.status !== "ACTIVITY_STATUS_COMPLETED") { - throw new BaseError( - JSON.stringify({ - message: `Activity is not yet completed: ${activity.status}`, - activityId: activity.id, - activityStatus: activity.status as TActivityStatus, - }) - ); + throw new TurnkeyActivityError({ + message: `Activity is not yet completed: ${activity.status}`, + activityId: activity.id, + activityStatus: activity.status as TActivityStatus, + }); } const { signedTransaction } = activity.result?.signTransactionResult!; @@ -358,12 +350,9 @@ async function signTransactionWithErrorWrapping( signWith ); } catch (error) { - throw new BaseError( - JSON.stringify({ - message: `Failed to sign transaction: ${(error as Error).message}`, - cause: error as Error, - }) - ); + throw new BaseError(`Failed to sign transaction`, { + details: JSON.stringify(error), + }); } return `0x${signedTx}`; @@ -390,14 +379,12 @@ async function signTransactionImpl( const { id, status, type } = activity; if (activity.status !== "ACTIVITY_STATUS_COMPLETED") { - throw new BaseError( - JSON.stringify({ - message: `Unexpected activity status: ${activity.status}`, - activityId: id, - activityStatus: status, - activityType: type, - }) - ); + throw new TurnkeyActivityError({ + message: `Unexpected activity status: ${activity.status}`, + activityId: id, + activityStatus: status, + activityType: type, + }); } return assertNonNull( @@ -411,13 +398,11 @@ async function signTransactionImpl( }); if (activity.status !== "ACTIVITY_STATUS_COMPLETED") { - throw new BaseError( - JSON.stringify({ - message: `Unexpected activity status: ${activity.status}`, - activityId: activity.id, - activityStatus: activity.status as TActivityStatus, - }) - ); + throw new TurnkeyActivityError({ + message: `Unexpected activity status: ${activity.status}`, + activityId: activity.id, + activityStatus: activity.status as TActivityStatus, + }); } return assertNonNull(signedTransaction); @@ -439,12 +424,9 @@ async function signMessageWithErrorWrapping( signWith ); } catch (error) { - throw new BaseError( - JSON.stringify({ - message: `Failed to sign: ${(error as Error).message}`, - cause: error as Error, - }) - ); + throw new BaseError(`Failed to sign message`, { + details: JSON.stringify(error), + }); } return signedMessage as Hex; @@ -474,14 +456,12 @@ async function signMessageImpl( const { id, status, type } = activity; if (status !== "ACTIVITY_STATUS_COMPLETED") { - throw new BaseError( - JSON.stringify({ - message: `Unexpected activity status: ${activity.status}`, - activityId: id, - activityStatus: status, - activityType: type, - }) - ); + throw new TurnkeyActivityError({ + message: `Unexpected activity status: ${activity.status}`, + activityId: id, + activityStatus: status, + activityType: type, + }); } result = assertNonNull(activity?.result?.signRawPayloadResult); @@ -494,13 +474,11 @@ async function signMessageImpl( }); if (activity.status !== "ACTIVITY_STATUS_COMPLETED") { - throw new BaseError( - JSON.stringify({ - message: `Unexpected activity status: ${activity.status}`, - activityId: activity.id, - activityStatus: activity.status as TActivityStatus, - }) - ); + throw new TurnkeyActivityError({ + message: `Unexpected activity status: ${activity.status}`, + activityId: activity.id, + activityStatus: activity.status as TActivityStatus, + }); } result = {