You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(errors): Integrate miette for enhanced diagnostic reporting (#10241)
# Description
SWC api `try_with_handler` only return anyhow!(msg) Err, it'a will lose many message about error stack, level ...,
To express more error message in swc, we can return `Vec<Diagnostic>` in `try_with_handler`;
try_with_handler_diagnostic return a `Result<Ret, Vec<Diagnostic>>`. Later user can using `diagnostic.code` to distinguish what kind error is.
- Added `miette` as a dependency for improved error handling and reporting.
- Updated diagnostic handling to utilize `miette` for pretty printing errors.
- Introduced a new `ThreadSafetyDiagnostics` to manage and store diagnostics.
- Refactored emitters to support the new diagnostic structure and ensure compatibility with `miette`.
- Enhanced error handling in various components to provide clearer and more informative error messages.
# BREAKING CHANGE:
The api `try_with_handler` would not return anyhow!(msg).
It's will return WrapperDiagnostics as it's Err. Then you can take
diagnostics from WrapperDiagnostics or get pretty string from it.
```diff
- pub fn try_with_handler<F, Ret>(cm: Lrc<SourceMap>, config: HandlerOpts, op:F) -> Result<Ret, anyhow::error>;
+ pub fn try_with_handler<F, Ret>(cm: Lrc<SourceMap>, config: HandlerOpts, op:F) -> Result<Ret, WrapperDiagnostics>;
```
```rust
/// A wrapper around a value that also contains a list of diagnostics.
/// It helps swc error system migrate to the new error reporting system.
pub struct WrapperDiagnostics {
diagnostics: Vec<SwcDignostic>,
....
}
impl WrapperDiagnostics {
pub fn diagnostics(&self) -> &[Diagnostic] {
&self.diagnostics
}
pub fn to_pretty_error(&self) -> anyhow::Error {
let error_msg = self.to_pretty_string();
anyhow::anyhow!(error_msg)
}
pub fn to_pretty_string(&self) -> String {
....
}
}
```
### formatting
In SWC, the `Syntax Error` message formatting dependents anyhow.
```rust
// input: Foo {}
// https://play.swc.rs/?version=1.11.13&code=H4sIAAAAAAAAA3PLz1eorgUAR6TWygYAAAA%3D&config=H4sIAAAAAAAAA1WPSw7DIAwF9zkF8rrbdtE79BAWdSIifrKJVBTl7iUE0maH3xsz8jooBbNoeKq1PMsQkYX4nEsi2Sf8lARIOxTNJia49XaWvRrRCtVoOxpIyBOluiX3hoMNQajjLXPGmzH%2FC3VwkUnkCu4o%2BsnSVTc0JbjwXmrZDkk50qF%2FwA%2FqsvNjMPLqm4kXGrYvhlQioBQBAAA%3D
x Expected ';', '}' or <eof>
,-[input.js:1:1]
1 | Foo {}
: ^|^ ^
: `-- This is the expression part of an expression statement
`----
Caused by:
0: failed to process js file
1: Syntax Error
```
So if we return `Vec<Diagnostic>` instead of `anyhow:Error`, The output
formatting would have a litter breaking change.
```diff
x Expected ';', '}' or <eof>
,-[input.js:1:1]
1 | Foo {}
: ^|^ ^
: `-- This is the expression part of an expression statement
`----
- Caused by:
- 0: failed to process js file
- 1: Syntax Error
+ x Syntax Error
```
# Related issue:
- Closes#10192
---------
Co-authored-by: Donny/강동윤 <[email protected]>
0 commit comments