slint-ui/slint · error · js_sys::Error

{err}

Error message

{err}

What it means

In tools/lsp/wasm_main.rs, process_preview_to_lsp_message handles the PreviewTypeChanged message by calling set_local_target on the primary preview; on failure the returned error is converted into a js_sys::Error whose message is just the Display of the err ("{err}") and propagated with `?` to JS. The browser sees a generic JS Error carrying the stringified internal failure (e.g. an invalid or unsupported preview target).

Solutions

  1. Check the preview target value sent from JS is one the WASM build supports before sending PreviewTypeChanged.
  2. Rebuild the WASM bundle with the required preview target feature enabled, or restrict the UI's target selector to supported values.
  3. In JS, wrap the call in try/catch and inspect error.message (the stringified err) to surface which target failed.

Example fix

// before (JS)
lsp.setPreviewTarget("winit");
// after (JS)
try {
  lsp.setPreviewTarget("wasm" /* supported target */);
} catch (e) {
  console.error("Preview target change failed:", e.message);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// JS side, before sending the message
const supported = ["wasm", "browser"]; // targets compiled into this WASM build
if (!supported.includes(target)) {
  throw new Error(`Preview target "${target}" not supported by this build`);
}
lsp.processMessage({ PreviewTypeChanged: { target } });

Type guard

function isSupportedTarget(t) { return typeof t === "string" && SUPPORTED_TARGETS.includes(t); }

Try / catch

try {
  processPreviewToLspMessage({ PreviewTypeChanged: { target } });
} catch (e) {
  console.error("set_local_target failed:", e.message); // message is the stringified internal err
}

Prevention

When it happens

Trigger: Calling the WASM LSP entry with message M::PreviewTypeChanged whose target value is rejected by set_local_target (e.g. a target not supported in the current environment/build), causing map_err to wrap it in js_sys::Error and early-return from the public function.

Common situations: A frontend (SlintPad / VS Code web extension) switches preview type to a target unavailable in the WASM build (e.g. a renderer or platform target compiled out), so the local target cannot be applied.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of slint-ui/slint@bb937076de (2026-09-16). Data as JSON: /api/errors/ea35be3d9ec47ca2. Report an issue: GitHub.

Appendix: source

Thrown at tools/lsp/wasm_main.rs:331

                    &ctx.server_notifier,
                    uri,
                    version,
                    diagnostics,
                );
            }
            M::ShowDocument { file, selection, .. } => {
                let sn = ctx.server_notifier.clone();
                wasm_bindgen_futures::spawn_local(async move {
                    crate::lsp_to_editor::send_show_document_to_editor(sn, file, selection, true)
                        .await
                });
            }
            M::PreviewTypeChanged { target } => {
                ctx.session
                    .primary_preview()
                    .to_preview
                    .set_local_target(target)
                    .map_err(|err| js_sys::Error::new(&format!("{err}")))?;
            }
            M::RequestState { files, settings } => {
                crate::language::send_requested_state_to_preview(&ctx, &files, &settings);
            }
            M::UpdateUserSettings { name, contents } => {
                crate::language::store_user_settings(&name, &contents);
            }
            M::SendWorkspaceEdit { label, edit } => {
                forward_workspace_edit(ctx.server_notifier.clone(), label, Ok(edit));
            }
            M::SendShowMessage { message } => {
                let _ = ctx
                    .server_notifier
                    .send_notification::<lsp_types::notification::ShowMessage>(message);
            }
            M::TelemetryEvent(object) => {
                let _ = ctx
                    .server_notifier

View on GitHub (pinned to bb937076de)