Skip to content

feat(web): expose stable errorCode on thrown JS errors for variant dispatch - #2060

Closed
WiktorStarczewski wants to merge 2 commits into
wiktor/apply-tx-after-submit-failedfrom
wiktor/error-code-dispatch
Closed

feat(web): expose stable errorCode on thrown JS errors for variant dispatch#2060
WiktorStarczewski wants to merge 2 commits into
wiktor/apply-tx-after-submit-failedfrom
wiktor/error-code-dispatch

Conversation

@WiktorStarczewski

@WiktorStarczewski WiktorStarczewski commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

Closes 0xMiden/web-sdk#123

Summary

  • Adds a stable errorCode string property to JS errors thrown by the WebClient
  • Consumers can dispatch on err.errorCode === 'ApplyTransactionAfterSubmitFailed' instead of parsing error message strings
  • Stability contract: code strings never change once added

Background

JS consumers of the SDK need to programmatically dispatch on error types. Parsing error message strings is fragile (not stable across versions, verbose, localization-unfriendly). During wallet hardening, every catch block needed to determine the error variant — string matching broke across SDK updates and was unreliable for multi-line messages.

Changes

  • crates/web-client/src/lib.rs: New error_code_from_client_error() function that maps ClientError variants to stable string codes. Currently exposed:

    • ApplyTransactionAfterSubmitFailed — tx on chain, apply failed locally
    • AccountLocked — account state out of sync
    • NoteNotFoundOnChain — note not committed yet
    • RpcError — network/node issue

    The code is attached as a property on the thrown JsValue error object. New codes can be added as consumers need them — the contract is that existing codes never change.

How the wallet uses it

The wallet's TransactionProcessor dispatches on err.errorCode === 'ApplyTransactionAfterSubmitFailed' to mark submitted-but-unapplied transactions as Completed rather than Failed. This replaced fragile string matching and is resilient to SDK error message changes.

Depends on #2059 which adds the ApplyTransactionAfterSubmitFailed error variant that this PR dispatches on.

Test plan

  • Existing tests pass
  • Error thrown from ApplyTransactionAfterSubmitFailed path has errorCode property set to "ApplyTransactionAfterSubmitFailed"
  • Error thrown from AccountLocked path has errorCode === "AccountLocked"
  • Error from an unmapped variant has no errorCode property (or undefined)

Note

The web-sdk parts of this PR have been migrated to 0xMiden/web-sdk#25 as part of the web-sdk split (#1992 / #2135). The miden-client side stays here. The web-sdk PR depends on #2059 landing first.

@SantiagoPittella SantiagoPittella left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It would be awesome to have a test of this

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It would be awesome to have a test for this

Comment on lines +392 to +405
fn error_code_from_client_error(err: &ClientError) -> Option<&'static str> {
// Only include variants consumers are known to dispatch on. Others
// can be added as new callers need them — the stability contract is
// "code string never changes once added."
match err {
ClientError::ApplyTransactionAfterSubmitFailed { .. } => {
Some("ApplyTransactionAfterSubmitFailed")
},
ClientError::AccountLocked(_) => Some("AccountLocked"),
ClientError::NoteNotFoundOnChain(_) => Some("NoteNotFoundOnChain"),
ClientError::RpcError(_) => Some("RpcError"),
_ => None,
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

When someone adds a new ClientError variant that JS should dispatch on, the compiler won't remind them. We might want to use an enum containing the variants that we want to expose as JS errors, that way we could enforce the conversion at compile time.

@WiktorStarczewski

Copy link
Copy Markdown
Contributor Author

Heads-up: the web-sdk-related changes from this PR have been migrated to a new PR on the web-sdk repo: 0xMiden/web-sdk#25.

This is part of the ongoing split of web/WASM components from miden-client into a dedicated repo (#1992 / #2135). Once that split lands, the crates/web-client/ and crates/idxdb-store/ directories will no longer exist in miden-client, so the migration moves those changes to the right home now.

Please continue this PR with the miden-client-only changes; the web-sdk-side changes are tracked in 0xMiden/web-sdk#25. If you have write access to your branch, the cleanest follow-up is to drop the web-sdk file changes from this PR. Otherwise they'll naturally fall out when you rebase after #1992 / #2135 merges.

Note: this PR is stacked on #2059 (miden-client-only). The migrated web-sdk PR depends on #2059's ApplyTransactionAfterSubmitFailed error contract landing first.

WiktorStarczewski added a commit to 0xMiden/web-sdk that referenced this pull request Apr 28, 2026
…iant dispatch

Migrated from 0xMiden/rust-sdk#2060 (author: WiktorStarczewski) as
part of the web-sdk split. Original PR: 0xMiden/rust-sdk#2060

Depends on the ApplyTransactionAfterSubmitFailed error variant added by
miden-client #2059 — landing this requires the upstream miden-client
dep on web-sdk's main to include that change first.
@SantiagoPittella

Copy link
Copy Markdown
Collaborator

Can we consider this closed @WiktorStarczewski ?

@juan518munoz

Copy link
Copy Markdown
Collaborator

@WiktorStarczewski this can be closed as it's already handled in the new repo, right?

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