use swc_common::{errors::HANDLER, Span, Spanned}; pub trait ErrReporter { fn err(&self, msg: &str); } impl ErrReporter for T where T: Spanned, { fn err(&self, msg: &str) { HANDLER.with(|h| h.span_err(self.span(), msg)); } } #[derive(Debug)] pub struct SpErr { pub span: Span, pub error: E, } impl SpErr where E: std::error::Error, { pub fn new(span: Span, error: E) -> Self { SpErr { span, error } } pub fn report(&self) { HANDLER.with(|handler| handler.span_err(self.span, &self.error.to_string())) } } impl std::error::Error for SpErr where E: std::error::Error, { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { self.error.source() } } impl std::fmt::Display for SpErr where E: std::fmt::Display, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(&self.error, f) } } pub trait ErrorWithSpanExt: std::error::Error + Sized { fn with_span(self, span: Span) -> SpErr { SpErr::new(span, self) } } impl ErrorWithSpanExt for T where T: std::error::Error {}