{"record":{"id":"b130c8b1287a4a57","repo":"QuipNetwork/hashsigs-rs","slug":"err-code-err-message","errorCode":null,"errorMessage":"[{err.code}] {err.message}","messagePattern":"\\[\\{err\\.code\\}\\] \\{err\\.message\\}","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"src/wasm/mod.rs","lineNumber":1684,"sourceCode":"}\n\n#[cfg(any(test, feature = \"wasm-bindings\"))]\nfn hex_string(bytes: &[u8]) -> String {\n    let mut out = String::from(\"0x\");\n    for byte in bytes {\n        use core::fmt::Write;\n        let _ = write!(out, \"{byte:02x}\");\n    }\n    out\n}\n\n#[cfg(feature = \"wasm-bindings\")]\nfn js_error(err: WasmErr) -> JsValue {\n    let e = js_sys::Error::new(&err.message);\n    if js_sys::Reflect::set(&e, &JsValue::from_str(\"code\"), &JsValue::from_str(err.code)).is_err() {\n        // Reflect::set failed, so the machine-readable code would be lost off the\n        // Error object. Fold it into the message so callers never lose it.\n        return js_sys::Error::new(&format!(\"[{}] {}\", err.code, err.message)).into();\n    }\n    e.into()\n}\n\n/// Build a serde-boundary error handler tagged with a STATIC argument label\n/// (e.g. `\"publicKey\"`). The label identifies which argument failed to\n/// deserialize without ever echoing the caller-supplied value (inputs include\n/// secret seeds). Returns a closure so call sites read\n/// `.map_err(js_error_from_serde(\"publicKey\"))`.\n#[cfg(feature = \"wasm-bindings\")]\nfn js_error_from_serde(label: &'static str) -> impl Fn(serde_wasm_bindgen::Error) -> JsValue {\n    move |_error| {\n        js_error(WasmErr {\n            code: ERR_INVALID_INPUT,\n            message: format!(\"invalid {label}: argument shape or field encoding\"),\n        })\n    }\n}","sourceCodeStart":1666,"sourceCodeEnd":1702,"githubUrl":"https://github.com/QuipNetwork/hashsigs-rs/blob/128c4ccb5c8ed038eb0b9953993dbbcd5c6c1b23/src/wasm/mod.rs#L1666-L1702","documentation":"This is the fallback message format `[CODE] message` produced by js_error in src/wasm/mod.rs when js_sys::Reflect::set fails to attach the `code` property to the JS Error object. The library folds the machine-readable code into the message string so the code is never lost, at the cost of a non-standard message shape.","triggerScenarios":"A WasmErr crosses the wasm->JS boundary while Reflect::set on the Error object returns an error (e.g. exotic JS hosts/proxies where the Error object rejects new property definitions); the returned Error's message becomes \"[code] err.message\" instead of plain err.message.","commonSituations":"Running the wasm module in hardened or sandboxed JS environments (frozen objects, unusual realms) where property definition on Error instances fails; code that matches on exact message text breaks because messages now carry a `[CODE] ` prefix; tooling that relies on e.code gets undefined.","solutions":["Parse the code out of the message with /^\\[(.+?)\\] / when e.code is undefined.","Prefer matching on the code (from e.code or the parsed prefix) rather than the full message string.","Verify the JS runtime allows defining properties on Error instances; test Reflect behavior if embedding in a custom host.","Report/upgrade if your host makes Reflect::set fail, since it degrades the API surface."],"exampleFix":"// before\nif (e.message === \"invalid public key\") { ... }\n// after\nconst m = /^\\[(.+?)\\] /.exec(e.message);\nconst code = e.code ?? m?.[1];\nif (code === \"INVALID_PUBLIC_KEY\") { ... }","handlingStrategy":"fallback","validationCode":"function extractWasmCode(e) {\n  if (typeof e?.code === 'string') return e.code;\n  const m = /^\\[(.+?)\\] /.exec(e?.message ?? '');\n  return m ? m[1] : null;\n}","typeGuard":"function hasFoldedCode(e) {\n  return e instanceof Error && e.code === undefined && /^\\[.+?\\] /.test(e.message);\n}","tryCatchPattern":"try {\n  wasmApi(input);\n} catch (e) {\n  const code = extractWasmCode(e); // works for both property and folded message\n  if (code === null) throw e;\n  dispatchByCode(code, e.message.replace(/^\\[.+?\\] /, ''));\n}","preventionTips":["Never string-match the exact error message; match on the code parsed from e.code or the `[CODE] ` prefix.","Test your host environment once for Reflect.set support on Error instances to know which message shape to expect.","Centralize error-code extraction in one helper so both message shapes are handled consistently.","Avoid freezing/sealing Error objects or using proxy hosts that block property definition when loading wasm modules."],"tags":["wasm","javascript","fallback-message","wasm-bindings"],"backgroundTag":"api-error-response","analyzedSha":"128c4ccb5c8ed038eb0b9953993dbbcd5c6c1b23","analyzedAt":"2026-09-08T21:14:08.025Z","contentChangedAt":"2026-09-08T21:14:08.025Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}