use crate::{ShellError, Span};
use miette::{Diagnostic, LabeledSpan, Severity, SourceSpan};
use std::{
error::Error as StdError,
fmt::{self, Display},
};
#[derive(Debug, Clone, PartialEq)]
pub struct EvalBlockWithInputError {
pub span: Span,
pub label: &'static str,
pub error: Box<ShellError>,
}
impl EvalBlockWithInputError {
pub const SOURCE_VALUE: &'static str = "source value";
pub const CALLER: &'static str = "error happened when running this";
pub fn wrap(error: ShellError, span: Span, label: &'static str) -> ShellError {
if error.is_interrupt() || matches!(error, ShellError::Exit { .. }) {
return error;
}
ShellError::EvalBlockWithInput(Self {
span,
label,
error: Box::new(error),
})
}
fn inline_error(&self) -> Option<&ShellError> {
self.error.source_code().is_none().then_some(&*self.error)
}
}
impl Display for EvalBlockWithInputError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.error, f)
}
}
impl StdError for EvalBlockWithInputError {}
impl Diagnostic for EvalBlockWithInputError {
fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.error.code()
}
fn severity(&self) -> Option<Severity> {
self.error.severity()
}
fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.inline_error().and_then(|error| error.help())
}
fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.inline_error().and_then(|error| error.url())
}
fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
let mut labels: Vec<_> = self
.inline_error()
.and_then(|error| error.labels())
.into_iter()
.flatten()
.collect();
let span = SourceSpan::from(self.span);
let already_labeled = labels.iter().any(|label| {
*label.inner() == span
|| (self.label == Self::SOURCE_VALUE && label.label() == Some(Self::SOURCE_VALUE))
});
if !already_labeled {
labels.push(LabeledSpan::new_with_span(Some(self.label.into()), span));
}
Some(Box::new(labels.into_iter()))
}
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
match self.inline_error() {
Some(error) => error.related(),
None => Some(Box::new(std::iter::once(&*self.error as &dyn Diagnostic))),
}
}
fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
self.inline_error()
.and_then(|error| error.diagnostic_source())
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::shell_error::generic::GenericError;
fn input_error(error: ShellError, span: Span) -> ShellError {
EvalBlockWithInputError::wrap(error, span, EvalBlockWithInputError::SOURCE_VALUE)
}
fn label_texts(error: &dyn Diagnostic) -> Vec<Option<String>> {
error
.labels()
.into_iter()
.flatten()
.map(|label| label.label().map(str::to_string))
.collect()
}
#[test]
fn reports_as_the_wrapped_error() {
let inner = GenericError::new("boom", "here", Span::new(10, 12)).with_code("my::code");
let error = input_error(inner.into(), Span::new(1, 2));
assert_eq!(error.to_string(), "boom");
assert_eq!(
error.code().map(|code| code.to_string()),
Some("my::code".into())
);
assert_eq!(
label_texts(&error),
[Some("here".into()), Some("source value".into())]
);
}
#[test]
fn reports_wrapped_error_with_own_source_as_related() {
let inner = ShellError::from(GenericError::new_internal("boom", "here"));
let error = input_error(inner.clone(), Span::new(1, 2));
assert_eq!(error.to_string(), "boom");
assert_eq!(label_texts(&error), [Some("source value".into())]);
assert!(error.source_code().is_none());
let related: Vec<_> = error.related().into_iter().flatten().collect();
assert_eq!(related.len(), 1);
assert_eq!(related[0].to_string(), "boom");
assert_eq!(label_texts(related[0]), [Some("here".into())]);
}
#[test]
fn labels_only_the_innermost_input_value() {
let inner = GenericError::new("boom", "here", Span::new(10, 12));
let error = input_error(input_error(inner.into(), Span::new(5, 6)), Span::new(1, 2));
let spans: Vec<_> = error
.labels()
.into_iter()
.flatten()
.map(|label| *label.inner())
.collect();
assert_eq!(spans, [Span::new(10, 12).into(), Span::new(5, 6).into()]);
}
#[test]
fn labels_each_caller_once() {
let inner = GenericError::new("boom", "here", Span::new(10, 12));
let caller = |error, span| {
EvalBlockWithInputError::wrap(error, span, EvalBlockWithInputError::CALLER)
};
let error = caller(caller(inner.into(), Span::new(10, 12)), Span::new(1, 2));
assert_eq!(
label_texts(&error),
[
Some("here".into()),
Some("error happened when running this".into())
]
);
}
#[test]
fn passes_control_flow_through() {
let exit = ShellError::Exit {
code: 4,
abort: false,
};
assert_eq!(input_error(exit.clone(), Span::new(1, 2)), exit);
let interrupted = ShellError::Interrupted {
span: Span::new(3, 4),
};
assert_eq!(
input_error(interrupted.clone(), Span::new(1, 2)),
interrupted
);
}
}