{"record":{"id":"30822f23711eea11","repo":"astral-sh/ruff","slug":"err-30822f","errorCode":null,"errorMessage":"{err}","messagePattern":"\\{err\\}","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"crates/ruff_wasm/src/lib.rs","lineNumber":520,"sourceCode":"        Ok(comments)\n    }\n\n    /// Parses the content and returns its AST\n    pub fn parse(&self, contents: &str) -> Result<String, Error> {\n        let parsed = parse_unchecked(contents, ParseOptions::from(Mode::Module));\n\n        Ok(format!(\"{:#?}\", parsed.into_syntax()))\n    }\n\n    pub fn tokens(&self, contents: &str) -> Result<String, Error> {\n        let parsed = parse_unchecked(contents, ParseOptions::from(Mode::Module));\n\n        Ok(format!(\"{:#?}\", parsed.tokens().as_ref()))\n    }\n}\n\npub(crate) fn into_error<E: std::fmt::Display>(err: E) -> Error {\n    Error::new(&err.to_string())\n}\n\nstruct ParsedModule<'a> {\n    source_code: &'a str,\n    parsed: Parsed<Mod>,\n    trivia_ranges: TriviaRanges,\n}\n\nimpl<'a> ParsedModule<'a> {\n    fn from_source(source_code: &'a str) -> Result<Self, Error> {\n        let parsed = parse(source_code, ParseOptions::from(Mode::Module)).map_err(into_error)?;\n        let trivia_ranges = TriviaRanges::from(parsed.tokens());\n        Ok(Self {\n            source_code,\n            parsed,\n            trivia_ranges,\n        })\n    }","sourceCodeStart":502,"sourceCodeEnd":538,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_wasm/src/lib.rs#L502-L538","documentation":"In ruff's WASM bindings, `into_error` converts any `Display`-able error into the wasm-bindgen `JsError` (`Error::new(&err.to_string())`). Any failure inside the exported WASM API (parse, tokenize, lint of a Python source string) is surfaced to JavaScript as a thrown JS Error whose message is the Rust error's Display text.","triggerScenarios":"Calling a WASM export (e.g. the Playwright/Playground parse or check API) with input that makes the underlying Rust code return `Err`, which is funneled through `into_error` at lib.rs:520 — e.g. invalid Python source causing an unrecoverable parse error path, or a panic converted via catch_unwind.","commonSituations":"Ruff Playground or the VS Code web extension feeding malformed or unsupported Python code into the WASM module; out-of-memory on huge inputs; passing invalid UTF-16 offsets into position-mapping APIs.","solutions":["Inspect the thrown JS Error message — it contains the Rust-side Display text describing the real failure.","Validate/normalize the input source string and parameters before calling the WASM API.","Catch the exception at the JS call site and degrade gracefully (the module itself stays usable).","If the input is valid Python but still fails, report it upstream with a minimal reproducer."],"exampleFix":"// before (JS)\nconst tokens = wasm.tokenize(source)\n// after (JS)\nlet tokens\ntry {\n  tokens = wasm.tokenize(source)\n} catch (e) {\n  console.error('ruff wasm failed:', e.message)\n  tokens = []\n}","handlingStrategy":"try-catch","validationCode":"// JS: validate input before calling the WASM API\nif (typeof source !== 'string') throw new TypeError('source must be a string')\nif (source.length > MAX_SOURCE_LENGTH) throw new RangeError('source too large')","typeGuard":"function isWasmError(e) {\n  return e instanceof Error && typeof e.message === 'string'\n}","tryCatchPattern":"try {\n  result = wasmModule.parse(source)\n} catch (e) {\n  // e.message contains the Rust Display text from into_error\n  console.error('ruff wasm:', e.message)\n  result = null\n}","preventionTips":["Always wrap WASM export calls in try/catch on the JS side.","Sanitize source strings and offsets before invoking the module.","Test with the Playground before shipping inputs to embedded WASM builds."],"tags":["wasm","javascript","parser","error-boundary"],"backgroundTag":"wasm-binding-error","analyzedSha":"26f38c119cac42e4d320ba08f09224fdec74af2c","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}