Skip to content

Commit

Permalink
improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Sep 9, 2024
1 parent 92c5305 commit f7fcad9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 76 deletions.
7 changes: 1 addition & 6 deletions examples/with-viem/src/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand Down
118 changes: 48 additions & 70 deletions packages/viem/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down Expand Up @@ -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!;
Expand All @@ -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}`;
Expand All @@ -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(
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 = {
Expand Down

0 comments on commit f7fcad9

Please sign in to comment.