Comments (3)
Ok, I've managed to do this, using a custom WithSourceCode
use std::fmt::{self, Display};
use miette::{Diagnostic, LabeledSpan, MietteHandler, ReportHandler, SourceCode};
/// Struct to report errors
pub struct Reporter;
impl Default for Reporter {
fn default() -> Self {
Self
}
}
impl ReportHandler for Reporter {
fn debug(&self, error: &(dyn miette::Diagnostic), f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
return fmt::Debug::fmt(error, f);
}
let handler = MietteHandler::default();
// Check that this is an error vector.
// We want to threat it as just a collection of unrelated errors
if error.to_string().is_empty() {
if let Some(source_code) = error.source_code() {
for e in error.related().unwrap() {
handler.debug(
&WithSourceCode {
diagnostic: e,
source_code,
},
f,
)?;
}
} else {
for e in error.related().unwrap() {
handler.debug(e, f)?;
}
}
Ok(())
} else {
handler.debug(error, f)
}
}
}
struct WithSourceCode<'d, 's> {
diagnostic: &'d dyn Diagnostic,
source_code: &'s dyn SourceCode,
}
impl Display for WithSourceCode<'_, '_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.diagnostic)
}
}
impl fmt::Debug for WithSourceCode<'_, '_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
std::fmt::Debug::fmt(&self.diagnostic, f)
}
}
impl std::error::Error for WithSourceCode<'_, '_> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.diagnostic.source()
}
}
impl Diagnostic for WithSourceCode<'_, '_> {
fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.diagnostic.code()
}
fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
self.diagnostic.diagnostic_source()
}
fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.diagnostic.help()
}
fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
self.diagnostic.labels()
}
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
self.diagnostic.related()
}
fn severity(&self) -> Option<miette::Severity> {
self.diagnostic.severity()
}
fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.diagnostic.url()
}
fn source_code(&self) -> Option<&dyn SourceCode> {
self.diagnostic.source_code().or(Some(self.source_code))
}
}
I think, that &dyn/impl SourceCode
and &dyn/impl Diagnostic
should implement corresponding traits.
And Report should be a Diagnostic
:)
from miette.
I tried to use a custom ErrVec
error and special report handler, to no avail.
There are 3 problems I've faced while doing it:
- Can't create
Report
from&Diagnostic
to provide a source code => need to clone it - Can't pass
&dyn SourceCode
towith_source_code
, because it doesn't implement (why?)SourceCode
Report
doesn't implementDiagnostic
from miette.
I currently do this by putting #[related]
on a Vec<impl Diagnostic>
, in a separate wrapper error that holds each of the individual errors. However, the meaning of that is subtly different I'd say.
from miette.
Related Issues (20)
- Unclear which part of long lines is being pointed to HOT 1
- Change `link` label to something else HOT 1
- New Crates.io Release? HOT 3
- Potentially broken word wrapping on `main` HOT 3
- Update owo-colors to v4? HOT 5
- Can't get `syntect-highlighter` to work HOT 3
- Remove severity from diagnostic output HOT 1
- Add `UnwrapPretty` trait HOT 5
- Compile to wasm32-unknown-unknown / Compile without `terminal_size` HOT 4
- Errors with zero-length span and no context lines are mostly pointless HOT 1
- 要是错误提示能使用中文输出就好了! HOT 5
- Failing tests with `--all-features` HOT 1
- Feature request: output an error as `json::Value` or `serde_json::Value` directly HOT 2
- Feature request: Span line limit for diagnostics HOT 3
- Help: Sub Errors HOT 3
- Is there a reason that `Report` doesn't implement `Error` and `Diagnostic`? HOT 2
- Converting a `Report` to a `Box<dyn Diagnostic>` wipes all `Diagnostic` information HOT 1
- Improve compilation speed by reducing expanded llvm lines HOT 4
- Add `#[cold]` to some of the constructor functions HOT 3
- `#[label]` derive doesn't seem to work with `Option<T>` where `T` implements `Into<SourceSpan>` but not `Copy`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from miette.