Skip to content

Conversation

@chakra-guy
Copy link
Contributor

@chakra-guy chakra-guy commented Feb 3, 2026

Explanation

Problem

When users scan a QR code and reject the connection on MetaMask Mobile, the error was not propagated to the dApp. The connection would just hang instead of showing an error, and the QR modal would remain open.

Solution

Error Handling:

  • Handle error responses in initialConnectionMessageHandler (for initial QR connections)
  • Handle error responses in handleMessage (for ongoing requests)
  • Use @metamask/rpc-errors (providerErrors.custom()) for proper EIP-1193 compliant errors
  • Preserve wallet error codes when provided, default to 4001 (User Rejected Request)

Modal Behavior:

  • Close QR modal automatically when user rejects connection (call unload() on error)

Playground Improvements:

  • Display errors for Legacy EVM connections
  • Display errors for Wagmi connections (was only logging to console)

Code Quality:

  • Refactored initialConnectionMessageHandler to use early returns (reduced nesting from 5 to 2 levels)
  • Combined ID-based and method-based message matching into single flow
  • Removed manual error construction in favor of standard MetaMask error library

Testing

  • Tested QR rejection with Connect Multichain, Legacy EVM, and Wagmi
  • All 259 unit tests passing
  • Added new unit tests for error handling
  • Verified modal closes on rejection
  • Verified errors display in UI for all three connection methods

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed, highlighting breaking changes as necessary
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@chakra-guy chakra-guy requested a review from a team as a code owner February 3, 2026 11:52
@chakra-guy chakra-guy force-pushed the fix/qr-rejection-handling branch 2 times, most recently from c580866 to 23dfed3 Compare February 3, 2026 12:37
When users scan a QR code and reject the connection on MetaMask Mobile,
the rejection error was not being propagated to the dApp. This fix ensures
that error responses from the wallet are correctly identified and rejected
with EIP-1193 compliant error codes (4001 = User Rejected Request).

Changes:
- Add error handling to initialConnectionMessageHandler for initial connections
- Add error handling to handleMessage for ongoing requests
- Create EIP-1193 compliant errors with code property
- Update playground to display Legacy EVM errors in UI
- Add unit tests for MWPTransport error handling

Tested with:
- Connect Multichain (QR code flow)
- Connect Legacy EVM (QR code flow)
- Wagmi connector (QR code flow)
- Replace manual error construction with providerErrors.custom()
- Simplify initialConnectionMessageHandler with early returns
- Reduce nesting depth from 5 levels to 2 levels
- Remove code duplication between ID-based and method-based matching
- Preserve wallet error codes while defaulting to 4001 (User Rejected)
- Close QR modal automatically when user rejects connection
- Add wagmiError state to track Wagmi connection errors
- Display Wagmi errors in the error section alongside Multichain and Legacy EVM
- Document QR rejection error propagation fix in connect-multichain
- Document modal auto-close on rejection
- Document Wagmi error display fix in browser-playground
@chakra-guy chakra-guy force-pushed the fix/qr-rejection-handling branch from 23dfed3 to 8a253fb Compare February 3, 2026 12:37
Comment on lines +169 to +170
code: errorData.code ?? 4001,
message: errorData.message ?? 'Request rejected by user',
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure about these default values.

wow. so were we missing request.reject from errors this whole time? I guess that makes sense because I feel like the DefaultTransport has handling errors differently than the MWPTransport. sigh, good catch

Comment on lines +446 to +447
code: errorData.code ?? 4001,
message: errorData.message ?? 'Connection rejected',
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure about these defaults

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.

3 participants