{"record":{"id":"4b46ab4d84d401fb","repo":"swc-project/swc","slug":"pretty-diagnostics","errorCode":null,"errorMessage":"pretty_diagnostics","messagePattern":"pretty_diagnostics","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/swc_error_reporters/src/lib.rs","lineNumber":120,"sourceCode":"        let report_handler = to_pretty_handler(color);\n\n        self.diagnostics\n            .iter()\n            .map(|d| d.to_pretty_string(&self.cm, skip_filename, &report_handler))\n            .collect::<String>()\n    }\n}\n\nimpl TWithDiagnosticArray<anyhow::Error> {\n    /// Converts the diagnostics to a pretty error string and wraps it in an\n    /// anyhow::Error.\n    pub fn to_pretty_error(self) -> anyhow::Error {\n        let pretty_diagnostics = self.take_pretty_diagnostics();\n\n        match self.inner {\n            Some(inner_error) => inner_error.context(pretty_diagnostics),\n            None => {\n                anyhow!(pretty_diagnostics)\n            }\n        }\n    }\n\n    /// Converts the diagnostics to a pretty string representation.\n    pub fn to_pretty_string(self) -> String {\n        let pretty_error = self.to_pretty_error();\n\n        pretty_error.to_string()\n    }\n}\n","sourceCodeStart":102,"sourceCodeEnd":132,"githubUrl":"https://github.com/swc-project/swc/blob/d7d74346665e36e692aa07e13d4ee3ee692ee35c/crates/swc_error_reporters/src/lib.rs#L102-L132","documentation":"This is not a fixed message: TWithDiagnosticArray<anyhow::Error>::to_pretty_error converts collected diagnostics into a single anyhow::Error whose message is the pretty-printed diagnostic text (file/line/snippet). It is the standard way SWC collapses multiple transform diagnostics into one error; an inner error, if present, becomes the anyhow context chain.","triggerScenarios":"A transform pipeline step returned TWithDiagnosticArray with diagnostics attached (or an inner error plus diagnostics) and the caller invoked to_pretty_error()/to_pretty_string() - typically at the end of swc core transform or in @swc/core error handling.","commonSituations":"Any failing transform (type errors reported by a checker pass, plugin diagnostics, minifier errors) surfacing through this wrapper; users see the full multi-line diagnostic block as the error message.","solutions":["Read the pretty diagnostic body - it names the file, line, and the actual problem; fix the source or config it points at","If an inner error exists, print with {:#} (alternate formatting) to also see the context chain beneath the diagnostics","Do not string-match the whole message; key on structured diagnostics before conversion if you need programmatic handling"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Rust: this wrapper produces the pretty text as the error message\nlet out = transform(...);\nlet result: Result<Module, _> = out.map_err(|t| t.to_pretty_error());\nif let Err(pretty) = &result {\n    // `pretty`'s Display IS the formatted diagnostics; log it whole\n    tracing::error!(\"transform failed:\\n{pretty}\");\n}","preventionTips":["Keep the pretty string intact for logs; do not truncate or regex it for control flow","Inspect TWithDiagnosticArray.diagnostics before conversion when you need structured data (spans, codes)","Format with {:#} to include any inner error context chain"],"tags":["swc","diagnostics","error-reporting","anyhow"],"backgroundTag":null,"analyzedSha":"d7d74346665e36e692aa07e13d4ee3ee692ee35c","analyzedAt":"2026-08-16T12:41:13.160Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}