feat(web): expose stable errorCode on thrown JS errors for variant dispatch - #2060
feat(web): expose stable errorCode on thrown JS errors for variant dispatch#2060WiktorStarczewski wants to merge 2 commits into
Conversation
8bcef87 to
3cb3bbf
Compare
SantiagoPittella
left a comment
There was a problem hiding this comment.
It would be awesome to have a test of this
There was a problem hiding this comment.
It would be awesome to have a test for this
| 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, | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
|
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 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 |
…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.
|
Can we consider this closed @WiktorStarczewski ? |
|
@WiktorStarczewski this can be closed as it's already handled in the new repo, right? |
Closes 0xMiden/web-sdk#123
Summary
errorCodestring property to JS errors thrown by the WebClienterr.errorCode === 'ApplyTransactionAfterSubmitFailed'instead of parsing error message stringsBackground
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
catchblock 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: Newerror_code_from_client_error()function that mapsClientErrorvariants to stable string codes. Currently exposed:ApplyTransactionAfterSubmitFailed— tx on chain, apply failed locallyAccountLocked— account state out of syncNoteNotFoundOnChain— note not committed yetRpcError— network/node issueThe code is attached as a property on the thrown
JsValueerror 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
ApplyTransactionAfterSubmitFailederror variant that this PR dispatches on.Test plan
ApplyTransactionAfterSubmitFailedpath haserrorCodeproperty set to"ApplyTransactionAfterSubmitFailed"AccountLockedpath haserrorCode === "AccountLocked"errorCodeproperty (orundefined)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.