Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions yarn-project/ethereum/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,21 @@ function getNestedErrorData(error: unknown): string | undefined {
return undefined;
}

/**
* Truncates an error message to a safe length for log renderers.
* LogExplorer can only render up to 2500 characters in its summary view.
* We cap at 2000 to leave room for decorating context added by callers.
*/
function truncateErrorMessage(message: string): string {
const MAX = 2000;
const CHUNK = 950;
if (message.length <= MAX) {
return message;
}
const truncated = message.length - 2 * CHUNK;
return message.slice(0, CHUNK) + `...${truncated} characters truncated...` + message.slice(-CHUNK);
}

/**
* Formats a Viem error into a FormattedViemError instance.
* @param error - The error to format.
Expand Down Expand Up @@ -232,22 +247,10 @@ export function formatViemError(error: any, abi: Abi = ErrorsAbi): FormattedViem

// If it's a regular Error instance, return it with its message
if (error instanceof Error) {
return new FormattedViemError(error.message, (error as any)?.metaMessages);
}

const body = String(error);
const length = body.length;
// LogExplorer can only render up to 2500 characters in it's summary view. Try to keep the whole message below this number
// Limit the error to 2000 chacaters in order to allow code higher up to decorate this error with extra details (up to 500 characters)
if (length > 2000) {
const chunk = 950;
const truncated = length - 2 * chunk;
return new FormattedViemError(
body.slice(0, chunk) + `...${truncated} characters truncated...` + body.slice(-1 * chunk),
);
return new FormattedViemError(truncateErrorMessage(error.message), (error as any)?.metaMessages);
}

return new FormattedViemError(body);
return new FormattedViemError(truncateErrorMessage(String(error)));
}

function stripAbis(obj: any) {
Expand Down
Loading