Skip to content

Commit 29fe845

Browse files
authored
fix: stringify unknown errors better (#712)
<!-- start pr-codex --> ## PR-Codex overview This PR focuses on enhancing error handling in the codebase by introducing a new `prettifyError` function and updating existing error formatting to use this function. It also adds a `prettifyTransactionError` function for transaction-specific errors. ### Detailed summary - Added `prettifyError` function in `src/utils/error.ts` for improved error message formatting. - Removed the old `formatError` function in `src/server/middleware/error.ts`. - Updated error handling in `src/utils/transaction/queueTransaction.ts` to use `prettifyError`. - Updated error handling in `src/server/routes/contract/write/write.ts` to use `prettifyError`. - Updated error message formatting in `src/worker/tasks/sendTransactionWorker.ts` to use `prettifyError`. - Introduced `prettifyTransactionError` function in `src/utils/error.ts` for transaction error handling. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent cd02aa3 commit 29fe845

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed

src/server/middleware/error.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { FastifyInstance } from "fastify";
22
import { ReasonPhrases, StatusCodes } from "http-status-codes";
3-
import { stringify } from "thirdweb/utils";
43
import { ZodError } from "zod";
54
import { env } from "../../utils/env";
65
import { parseEthersError } from "../../utils/ethers";
@@ -22,13 +21,6 @@ export const createCustomError = (
2221
code,
2322
});
2423

25-
export function formatError(error: unknown) {
26-
if (error instanceof Error) {
27-
return error.message;
28-
}
29-
return stringify(error);
30-
}
31-
3224
export const customDateTimestampError = (date: string): CustomError =>
3325
createCustomError(
3426
`Invalid date: ${date}. Needs to new Date() / new Date().toISOstring() / new Date().getTime() / Unix Epoch`,

src/server/routes/contract/write/write.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { type Static, Type } from "@sinclair/typebox";
1+
import { Type, type Static } from "@sinclair/typebox";
22
import type { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
44
import { prepareContractCall, resolveMethod } from "thirdweb";
55
import type { AbiFunction } from "thirdweb/utils";
66
import { getContractV5 } from "../../../../utils/cache/getContractv5";
7+
import { prettifyError } from "../../../../utils/error";
78
import { queueTransaction } from "../../../../utils/transaction/queueTransation";
8-
import { createCustomError, formatError } from "../../../middleware/error";
9+
import { createCustomError } from "../../../middleware/error";
910
import { abiArraySchema } from "../../../schemas/contract";
1011
import {
1112
contractParamSchema,
@@ -85,7 +86,7 @@ export async function writeToContract(fastify: FastifyInstance) {
8586
method = await resolveMethod(functionName)(contract);
8687
} catch (e) {
8788
throw createCustomError(
88-
formatError(e),
89+
prettifyError(e),
8990
StatusCodes.BAD_REQUEST,
9091
"BAD_REQUEST",
9192
);

src/utils/error.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import { ethers } from "ethers";
22
import { getChainMetadata } from "thirdweb/chains";
3+
import { stringify } from "thirdweb/utils";
34
import { getChain } from "./chain";
45
import { isEthersErrorCode } from "./ethers";
56
import { doSimulateTransaction } from "./transaction/simulateQueuedTransaction";
67
import type { AnyTransaction } from "./transaction/types";
78

8-
export const prettifyError = async (
9+
export const prettifyError = (error: unknown): string => {
10+
if (error instanceof Error) {
11+
return error.message;
12+
}
13+
return stringify(error);
14+
};
15+
16+
export const prettifyTransactionError = async (
917
transaction: AnyTransaction,
1018
error: Error,
1119
): Promise<string> => {

src/utils/transaction/queueTransation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import {
66
type PreparedTransaction,
77
encode,
88
} from "thirdweb";
9-
import { stringify } from "thirdweb/utils";
109
import { createCustomError } from "../../server/middleware/error";
1110
import type { txOverridesWithValueSchema } from "../../server/schemas/txOverrides";
1211
import { parseTransactionOverrides } from "../../server/utils/transactionOverrides";
12+
import { prettifyError } from "../error";
1313
import { insertTransaction } from "./insertTransaction";
1414
import type { InsertedTransaction } from "./types";
1515

@@ -43,7 +43,7 @@ export async function queueTransaction(args: QueuedTransactionParams) {
4343
data = await encode(transaction);
4444
} catch (e) {
4545
throw createCustomError(
46-
stringify(e),
46+
prettifyError(e),
4747
StatusCodes.BAD_REQUEST,
4848
"BAD_REQUEST",
4949
);

src/worker/tasks/sendTransactionWorker.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
isNonceAlreadyUsedError,
2222
isReplacementGasFeeTooLow,
2323
prettifyError,
24+
prettifyTransactionError,
2425
} from "../../utils/error";
2526
import { getChecksumAddress } from "../../utils/primitiveTypes";
2627
import { recordMetrics } from "../../utils/prometheus";
@@ -151,7 +152,7 @@ const _sendUserOp = async (
151152
const erroredTransaction: ErroredTransaction = {
152153
...queuedTransaction,
153154
status: "errored",
154-
errorMessage: stringify(e),
155+
errorMessage: prettifyError(e),
155156
};
156157
job.log(
157158
`Failed to populate transaction: ${erroredTransaction.errorMessage}`,
@@ -231,7 +232,7 @@ const _sendTransaction = async (
231232
const erroredTransaction: ErroredTransaction = {
232233
...queuedTransaction,
233234
status: "errored",
234-
errorMessage: stringify(e),
235+
errorMessage: prettifyError(e),
235236
};
236237
job.log(
237238
`Failed to populate transaction: ${erroredTransaction.errorMessage}`,
@@ -476,7 +477,7 @@ export const initSendTransactionWorker = () => {
476477
const erroredTransaction: ErroredTransaction = {
477478
...transaction,
478479
status: "errored",
479-
errorMessage: await prettifyError(transaction, error),
480+
errorMessage: await prettifyTransactionError(transaction, error),
480481
};
481482
job.log(`Transaction errored: ${stringify(erroredTransaction)}`);
482483

0 commit comments

Comments
 (0)