Skip to content

fix: include BDK cause in PSBT build error message#594

Merged
cloudonshore merged 6 commits intomainfrom
fix/psbt-error-message-enrichment
Mar 31, 2026
Merged

fix: include BDK cause in PSBT build error message#594
cloudonshore merged 6 commits intomainfrom
fix/psbt-error-message-enrichment

Conversation

@cloudonshore
Copy link
Copy Markdown
Contributor

@cloudonshore cloudonshore commented Mar 26, 2026

Problem

The "Failed to build PSBT from template" error is one of the top errors in Unified SwapBridge (~7.5% of failures). The root cause from BDK (e.g. InsufficientFunds, OutputBelowDustLimit, NoUtxosSelected) is lost when crossing the snap JSON-RPC boundary because:

  1. HandlerMiddleware re-throws as InvalidParamsError which doesn't accept a cause parameter (line 71)
  2. emitTrackingError checks error.cause instanceof Error but BDK WASM errors are CodifiedError plain objects, not Error instances (line 308)

This makes it impossible to diagnose why the PSBT rebuild fails in Sentry or Mixpanel.

Solution

Append the BDK cause message to the ValidationError message string so it survives serialization through the RPC boundary and appears in Sentry/Mixpanel. Handles both Error instances and BDK's CodifiedError plain objects.

After this change, errors will appear as:

Validation failed: Failed to build PSBT from template: InsufficientFunds

instead of:

Validation failed: Failed to build PSBT from template

Test plan

  • Existing tests updated and passing
  • New test for non-Error (CodifiedError) cause extraction
  • Verify in Sentry that new errors include the BDK cause message after deployment

Note

Low Risk
Low risk: changes are limited to error-message composition and accompanying unit tests, with no effect on PSBT construction logic. Main potential impact is on any consumers asserting exact error strings.

Overview
PSBT build failures now surface the underlying cause message. When AccountUseCases.#fillPsbt fails to finish() a transaction, the thrown ValidationError message is updated from a generic "Failed to build PSBT from template" to include the caught error’s .message (or unknown cause when absent), ensuring the reason survives RPC serialization.

Tests are updated/added to cover Error causes, non-Error object causes, and missing-message causes, and the change is documented in the Unreleased changelog (with the snap manifest shasum updated for the new bundle).

Written by Cursor Bugbot for commit a0413df. This will update automatically on new commits. Configure here.

The "Failed to build PSBT from template" error loses its cause when
crossing the snap JSON-RPC boundary. This makes it impossible to
diagnose why BDK's transaction builder fails (e.g. InsufficientFunds,
OutputBelowDustLimit, NoUtxosSelected).

- Append the BDK cause message to the ValidationError message string
  so it survives serialization through HandlerMiddleware and appears
  in Sentry/Mixpanel
- Wrap BdkTxBuilderAdapter.finish() to convert CodifiedError (WASM
  plain object) into a real Error instance so instanceof checks work
@cloudonshore cloudonshore requested a review from a team as a code owner March 26, 2026 14:46
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Comment thread packages/snap/src/use-cases/AccountUseCases.ts Outdated
@cloudonshore cloudonshore merged commit fbf1cd5 into main Mar 31, 2026
17 checks passed
@cloudonshore cloudonshore deleted the fix/psbt-error-message-enrichment branch March 31, 2026 12:38
github-merge-queue bot pushed a commit to MetaMask/metamask-mobile that referenced this pull request Apr 1, 2026
## **Description**

Bumps `@metamask/bitcoin-wallet-snap` from `^1.10.0` to `^1.10.1`.

Release PR: MetaMask/snap-bitcoin-wallet#595

Notable change:
- [fix: include BDK cause in PSBT build error
message](MetaMask/snap-bitcoin-wallet#594) —
surfaces the underlying BDK root cause (e.g. `InsufficientFunds`,
`OutputBelowDustLimit`) in PSBT build error messages so they appear in
Sentry/Mixpanel instead of being lost at the RPC boundary.

## **Changelog**

CHANGELOG entry: Fixed Bitcoin PSBT build errors now include the
underlying cause for better diagnostics

## **Related issues**

Fixes:

## **Manual testing steps**

1. Create a Bitcoin account
2. Trigger a PSBT build failure (e.g. insufficient funds swap)
3. Verify the error message in Sentry/Mixpanel includes the BDK cause

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I've included tests if applicable
- [x] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Low risk dependency-only bump with no application code changes; any
impact is limited to behavior changes inside the updated snap package.
> 
> **Overview**
> **Bumps the Bitcoin snap dependency** by updating
`@metamask/bitcoin-wallet-snap` from `^1.10.0` to `^1.10.1` in
`package.json`.
> 
> Updates `yarn.lock` to resolve and checksum the new `1.10.1` release.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
d858417. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
github-merge-queue bot pushed a commit to MetaMask/metamask-extension that referenced this pull request Apr 1, 2026
## **Description**

Bumps `@metamask/bitcoin-wallet-snap` from `^1.10.0` to `^1.10.1`.

Release PR: MetaMask/snap-bitcoin-wallet#595

Notable change:
- [fix: include BDK cause in PSBT build error
message](MetaMask/snap-bitcoin-wallet#594) —
surfaces the underlying BDK root cause (e.g. `InsufficientFunds`,
`OutputBelowDustLimit`) in PSBT build error messages so they appear in
Sentry/Mixpanel instead of being lost at the RPC boundary.

## **Changelog**

CHANGELOG entry: Fixed Bitcoin PSBT build errors now include the
underlying cause for better diagnostics

## **Related issues**

Fixes:

## **Manual testing steps**

1. Create a Bitcoin account
2. Trigger a PSBT build failure (e.g. insufficient funds swap)
3. Verify the error message in Sentry/Mixpanel includes the BDK cause

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I've included tests if applicable
- [x] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Patch-level dependency bump for the Bitcoin snap; primary risk is
behavior changes in Bitcoin PSBT build/error propagation affecting
diagnostics or edge-case flows.
> 
> **Overview**
> Updates the extension’s bundled Bitcoin snap dependency by bumping
`@metamask/bitcoin-wallet-snap` from `^1.10.0` to `^1.10.1`, with the
corresponding `yarn.lock` resolution/checksum update.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
081af90. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
github-merge-queue bot pushed a commit to MetaMask/metamask-extension that referenced this pull request Apr 1, 2026
## **Description**

Bumps `@metamask/bitcoin-wallet-snap` from `^1.10.0` to `^1.10.1`.

Release PR: MetaMask/snap-bitcoin-wallet#595

Notable change:
- [fix: include BDK cause in PSBT build error
message](MetaMask/snap-bitcoin-wallet#594) —
surfaces the underlying BDK root cause (e.g. `InsufficientFunds`,
`OutputBelowDustLimit`) in PSBT build error messages so they appear in
Sentry/Mixpanel instead of being lost at the RPC boundary.

## **Changelog**

CHANGELOG entry: Fixed Bitcoin PSBT build errors now include the
underlying cause for better diagnostics

## **Related issues**

Fixes:

## **Manual testing steps**

1. Create a Bitcoin account
2. Trigger a PSBT build failure (e.g. insufficient funds swap)
3. Verify the error message in Sentry/Mixpanel includes the BDK cause

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I've included tests if applicable
- [x] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Patch-level dependency bump for the Bitcoin snap; primary risk is
behavior changes in Bitcoin PSBT build/error propagation affecting
diagnostics or edge-case flows.
> 
> **Overview**
> Updates the extension’s bundled Bitcoin snap dependency by bumping
`@metamask/bitcoin-wallet-snap` from `^1.10.0` to `^1.10.1`, with the
corresponding `yarn.lock` resolution/checksum update.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
081af90. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
github-merge-queue bot pushed a commit to MetaMask/metamask-extension that referenced this pull request Apr 1, 2026
## **Description**

Bumps `@metamask/bitcoin-wallet-snap` from `^1.10.0` to `^1.10.1`.

Release PR: MetaMask/snap-bitcoin-wallet#595

Notable change:
- [fix: include BDK cause in PSBT build error
message](MetaMask/snap-bitcoin-wallet#594) —
surfaces the underlying BDK root cause (e.g. `InsufficientFunds`,
`OutputBelowDustLimit`) in PSBT build error messages so they appear in
Sentry/Mixpanel instead of being lost at the RPC boundary.

## **Changelog**

CHANGELOG entry: Fixed Bitcoin PSBT build errors now include the
underlying cause for better diagnostics

## **Related issues**

Fixes:

## **Manual testing steps**

1. Create a Bitcoin account
2. Trigger a PSBT build failure (e.g. insufficient funds swap)
3. Verify the error message in Sentry/Mixpanel includes the BDK cause

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I've included tests if applicable
- [x] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Patch-level dependency bump for the Bitcoin snap; primary risk is
behavior changes in Bitcoin PSBT build/error propagation affecting
diagnostics or edge-case flows.
> 
> **Overview**
> Updates the extension’s bundled Bitcoin snap dependency by bumping
`@metamask/bitcoin-wallet-snap` from `^1.10.0` to `^1.10.1`, with the
corresponding `yarn.lock` resolution/checksum update.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
081af90. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
github-merge-queue bot pushed a commit to MetaMask/metamask-extension that referenced this pull request Apr 1, 2026
## **Description**

Bumps `@metamask/bitcoin-wallet-snap` from `^1.10.0` to `^1.10.1`.

Release PR: MetaMask/snap-bitcoin-wallet#595

Notable change:
- [fix: include BDK cause in PSBT build error
message](MetaMask/snap-bitcoin-wallet#594) —
surfaces the underlying BDK root cause (e.g. `InsufficientFunds`,
`OutputBelowDustLimit`) in PSBT build error messages so they appear in
Sentry/Mixpanel instead of being lost at the RPC boundary.

## **Changelog**

CHANGELOG entry: Fixed Bitcoin PSBT build errors now include the
underlying cause for better diagnostics

## **Related issues**

Fixes:

## **Manual testing steps**

1. Create a Bitcoin account
2. Trigger a PSBT build failure (e.g. insufficient funds swap)
3. Verify the error message in Sentry/Mixpanel includes the BDK cause

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I've included tests if applicable
- [x] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Patch-level dependency bump for the Bitcoin snap; primary risk is
behavior changes in Bitcoin PSBT build/error propagation affecting
diagnostics or edge-case flows.
> 
> **Overview**
> Updates the extension’s bundled Bitcoin snap dependency by bumping
`@metamask/bitcoin-wallet-snap` from `^1.10.0` to `^1.10.1`, with the
corresponding `yarn.lock` resolution/checksum update.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
081af90. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
github-merge-queue bot pushed a commit to MetaMask/metamask-extension that referenced this pull request Apr 2, 2026
## **Description**

Bumps `@metamask/bitcoin-wallet-snap` from `^1.10.0` to `^1.10.1`.

Release PR: MetaMask/snap-bitcoin-wallet#595

Notable change:
- [fix: include BDK cause in PSBT build error
message](MetaMask/snap-bitcoin-wallet#594) —
surfaces the underlying BDK root cause (e.g. `InsufficientFunds`,
`OutputBelowDustLimit`) in PSBT build error messages so they appear in
Sentry/Mixpanel instead of being lost at the RPC boundary.

## **Changelog**

CHANGELOG entry: Fixed Bitcoin PSBT build errors now include the
underlying cause for better diagnostics

## **Related issues**

Fixes:

## **Manual testing steps**

1. Create a Bitcoin account
2. Trigger a PSBT build failure (e.g. insufficient funds swap)
3. Verify the error message in Sentry/Mixpanel includes the BDK cause

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I've included tests if applicable
- [x] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Patch-level dependency bump for the Bitcoin snap; primary risk is
behavior changes in Bitcoin PSBT build/error propagation affecting
diagnostics or edge-case flows.
> 
> **Overview**
> Updates the extension’s bundled Bitcoin snap dependency by bumping
`@metamask/bitcoin-wallet-snap` from `^1.10.0` to `^1.10.1`, with the
corresponding `yarn.lock` resolution/checksum update.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
081af90. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
github-merge-queue bot pushed a commit to MetaMask/metamask-extension that referenced this pull request Apr 2, 2026
## **Description**

Bumps `@metamask/bitcoin-wallet-snap` from `^1.10.0` to `^1.10.1`.

Release PR: MetaMask/snap-bitcoin-wallet#595

Notable change:
- [fix: include BDK cause in PSBT build error
message](MetaMask/snap-bitcoin-wallet#594) —
surfaces the underlying BDK root cause (e.g. `InsufficientFunds`,
`OutputBelowDustLimit`) in PSBT build error messages so they appear in
Sentry/Mixpanel instead of being lost at the RPC boundary.

## **Changelog**

CHANGELOG entry: Fixed Bitcoin PSBT build errors now include the
underlying cause for better diagnostics

## **Related issues**

Fixes:

## **Manual testing steps**

1. Create a Bitcoin account
2. Trigger a PSBT build failure (e.g. insufficient funds swap)
3. Verify the error message in Sentry/Mixpanel includes the BDK cause

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I've included tests if applicable
- [x] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Patch-level dependency bump for the Bitcoin snap; primary risk is
behavior changes in Bitcoin PSBT build/error propagation affecting
diagnostics or edge-case flows.
> 
> **Overview**
> Updates the extension’s bundled Bitcoin snap dependency by bumping
`@metamask/bitcoin-wallet-snap` from `^1.10.0` to `^1.10.1`, with the
corresponding `yarn.lock` resolution/checksum update.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
081af90. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
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.

2 participants