{"record":{"id":"c2004294db2dd0be","repo":"swc-project/swc","slug":"failed-to-parse-input-as-document","errorCode":null,"errorMessage":"failed to parse input as document","messagePattern":"failed to parse input as document","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"bindings/binding_html_node/src/lib.rs","lineNumber":588,"sourceCode":"                    &fm,\n                    swc_html::parser::parser::ParserConfig {\n                        scripting_enabled,\n                        iframe_srcdoc: opts.iframe_srcdoc,\n                        ..Default::default()\n                    },\n                    &mut errors,\n                );\n\n                let document = match document {\n                    Ok(v) => v,\n                    Err(err) => {\n                        err.to_diagnostics(handler).emit();\n\n                        for err in errors {\n                            err.to_diagnostics(handler).emit();\n                        }\n\n                        bail!(\"failed to parse input as document\")\n                    }\n                };\n\n                (DocumentOrDocumentFragment::Document(document), None)\n            };\n\n            let mut returned_errors = None;\n\n            if !errors.is_empty() {\n                returned_errors = Some(Vec::with_capacity(errors.len()));\n\n                for err in errors {\n                    let mut buf = vec![];\n\n                    err.to_diagnostics(handler).buffer(&mut buf);\n\n                    for i in buf {\n                        returned_errors.as_mut().unwrap().push(Diagnostic {","sourceCodeStart":570,"sourceCodeEnd":606,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/bindings/binding_html_node/src/lib.rs#L570-L606","documentation":"The Node HTML binding's document mode calls parse_file_as_document; on a fatal parse failure it emits the parser error and all buffered recoverable errors as diagnostics first, then bails with this generic message. As with the fragment variant, the real cause is in the diagnostics printed before the error. HTML parsing is normally error-tolerant, so a fatal result usually means structurally impossible input or a parser bug.","triggerScenarios":"Calling the HTML parse API without a context element with input that makes parse_file_as_document return Err, e.g. markup that breaks the document parsing state machine beyond recovery.","commonSituations":"Passing truncated or binary content where HTML was expected; feeding documents with constructs a specific swc_html version cannot recover from; upstream parser regressions after upgrading @swc/html.","solutions":["Inspect the diagnostics emitted just before the bail for exact positions and parser messages","Trim the document to the smallest repro around the first reported position","Re-try with the fragment API plus a context element if you only need a subtree","Report to swc_html if the input is valid HTML and diagnostics look wrong"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"function preflightDocument(input) {\n  if (typeof input !== 'string' || input.trim().length === 0) {\n    throw new TypeError('document input must be a non-empty string');\n  }\n  if (!/<[a-z!]/i.test(input)) {\n    throw new TypeError('input does not look like HTML');\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  const res = parseSync(src);\n} catch (e) {\n  if (String(e?.message).includes('failed to parse input as document')) {\n    return { ok: false, reason: 'invalid-html', raw: src };\n  }\n  throw e;\n}","preventionTips":["Reject empty and non-HTML payloads before calling the parser","Capture the binding's returned parse errors alongside the exception for reporting","Pin the @swc/html version in CI so parser behavior changes surface in upgrades, not production"],"tags":["swc","html","parser","document","node-bindings"],"backgroundTag":"html-parse-error","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}