Skip to content

feat: improve contract error message#5587

Open
cuzz-venus wants to merge 6 commits into
mainfrom
feat/improve-error-message
Open

feat: improve contract error message#5587
cuzz-venus wants to merge 6 commits into
mainfrom
feat/improve-error-message

Conversation

@cuzz-venus
Copy link
Copy Markdown
Contributor

@cuzz-venus cuzz-venus commented May 14, 2026

Jira ticket(s)

VPD-1166

Changes

  • Add parseContractError to decode viem contract reverts using a pre-bundled list of Venus ABIs, no ABI
    needed at call site
  • Add handleContractError to display contract reverts as a dedicated error toast (transparent + blur via
    tailwind override)
  • Add ContractErrorNotice component rendering the toast body: friendly phrase, error name + selector
    chip, copy/show-raw buttons
  • Add customErrorPhrases mapping known custom error names to friendly i18n strings
image

@vercel
Copy link
Copy Markdown

vercel Bot commented May 14, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
dapp-preview Ready Ready Preview May 14, 2026 1:06pm
dapp-testnet Ready Ready Preview May 14, 2026 1:06pm
venus.io Ready Ready Preview May 14, 2026 1:06pm

Request Review

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 14, 2026

🦋 Changeset detected

Latest commit: 111ec78

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@venusprotocol/evm Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 14, 2026

Coverage Report for ./apps/evm

Status Category Percentage Covered / Total
🔵 Lines 81.14% 45430 / 55984
🔵 Statements 81.14% 45430 / 55984
🔵 Functions 62.39% 652 / 1045
🔵 Branches 72.36% 5121 / 7077
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
apps/evm/src/libs/errors/customErrorPhrases.ts 100% 100% 100% 100%
apps/evm/src/libs/errors/ContractErrorNotice/index.tsx 100% 100% 100% 100%
apps/evm/src/libs/errors/handleContractError/index.tsx 48% 100% 0% 48% 16-29
apps/evm/src/libs/errors/handleContractError/parseContractError.ts 98.24% 86.36% 100% 98.24% 1
apps/evm/src/libs/errors/handleError/index.ts 85.36% 33.33% 100% 85.36% 1, 31-35
Generated in workflow #13439 for commit 111ec78 by the Vitest Coverage Report Action

@cuzz-venus cuzz-venus changed the title Feat/improve error message feat: improve contract error message May 14, 2026
@cuzz-venus
Copy link
Copy Markdown
Contributor Author

@greptile review this pr

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 14, 2026

Greptile Summary

This PR introduces a dedicated contract-error display layer: it decodes viem ContractFunctionRevertedError reverts (and raw hex revert data) against a pre-bundled set of Venus ABIs, maps known custom error names to friendly i18n strings, and renders a structured toast with copy/raw-toggle actions.

  • parseContractError walks the viem error chain via two paths (pre-decoded revert and raw hex ABI scan), falling back to UnknownContractError when the selector is unrecognised.
  • handleContractError + ContractErrorNotice render a non-auto-closing error toast with a friendly phrase, error chip, and raw-message expander; all UI strings correctly use useTranslation().
  • customErrorPhrases calls t() at module load time rather than at call time, meaning language switches after startup won't update the displayed phrases — this needs to be converted to a function called at error-display time.

Confidence Score: 4/5

Safe to merge after fixing the static translation map in customErrorPhrases.ts.

The translation phrases in customErrorPhrases are resolved once at module import time into fixed string primitives. A user who switches languages mid-session will continue to see error messages in the original language, making the i18n support effectively broken for that file. The rest of the pipeline — ABI-based decode, toast rendering, raw-error toggle — is well-structured and tested.

apps/evm/src/libs/errors/customErrorPhrases.ts and its consumer apps/evm/src/libs/errors/handleContractError/index.tsx (getFriendlyPhrase call site).

Important Files Changed

Filename Overview
apps/evm/src/libs/errors/customErrorPhrases.ts Calls t() at module-init time, freezing translations in the startup language — language switches won't update error phrases.
apps/evm/src/libs/errors/handleContractError/parseContractError.ts Cleanly implements a two-path decode strategy (pre-decoded viem revert and raw hex ABI scan); logic and types look correct.
apps/evm/src/libs/errors/handleContractError/index.tsx Correctly wires parsed contract error into displayNotification and logError; getFriendlyPhrase logic is straightforward.
apps/evm/src/libs/errors/handleError/index.ts Correctly slots contract-error handling before the generic error path; user-rejection and VError guards remain unaffected.
apps/evm/src/libs/errors/ContractErrorNotice/index.tsx UI component correctly uses useTranslation() hook and renders friendly phrase, error chip, copy, and raw-toggle buttons.
apps/evm/src/libs/errors/handleContractError/tests/parseContractError.spec.ts Good test coverage for pre-decoded, raw hex, unknown selector, and non-viem error paths.

Reviews (3): Last reviewed commit: "feat: update transacation && format type" | Re-trigger Greptile

Comment thread apps/evm/src/libs/translations/translations/th.json
Comment thread apps/evm/src/libs/errors/handleContractError/index.tsx
@cuzz-venus
Copy link
Copy Markdown
Contributor Author

@greptile review again

Comment on lines +1 to +10
import { t } from 'libs/translations';

export const customErrorPhrases: Record<string, string> = {
ActionPaused: t('contractErrors.actionPaused'),
InsufficientLiquidity: t('contractErrors.insufficientLiquidity'),
InsufficientCollateral: t('contractErrors.insufficientCollateral'),
SupplyCapExceeded: t('contractErrors.supplyCapExceeded'),
BorrowCapExceeded: t('contractErrors.borrowCapExceeded'),
TooMuchRepay: t('contractErrors.tooMuchRepay'),
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Stale translations on language switch

t() is called at module-initialization time, so the string values in customErrorPhrases are resolved once into the language that is active when the module is first imported. If a user subsequently switches languages, all error phrases from this map will still appear in the original language. The i18n-patterns guide mandates calling t() at render time (via useTranslation()) rather than at module load time. Converting this to a factory function and calling it inside getFriendlyPhrase fixes the problem.

Suggested change
import { t } from 'libs/translations';
export const customErrorPhrases: Record<string, string> = {
ActionPaused: t('contractErrors.actionPaused'),
InsufficientLiquidity: t('contractErrors.insufficientLiquidity'),
InsufficientCollateral: t('contractErrors.insufficientCollateral'),
SupplyCapExceeded: t('contractErrors.supplyCapExceeded'),
BorrowCapExceeded: t('contractErrors.borrowCapExceeded'),
TooMuchRepay: t('contractErrors.tooMuchRepay'),
};
import { t } from 'libs/translations';
export const getCustomErrorPhrases = (): Record<string, string> => ({
ActionPaused: t('contractErrors.actionPaused'),
InsufficientLiquidity: t('contractErrors.insufficientLiquidity'),
InsufficientCollateral: t('contractErrors.insufficientCollateral'),
SupplyCapExceeded: t('contractErrors.supplyCapExceeded'),
BorrowCapExceeded: t('contractErrors.borrowCapExceeded'),
TooMuchRepay: t('contractErrors.tooMuchRepay'),
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't fix - follows existing convention

@VenusProtocol VenusProtocol deleted a comment from greptile-apps Bot May 14, 2026
@cuzz-venus cuzz-venus requested a review from therealemjy May 14, 2026 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant