From e980b7237334b56f7b8c092956d35cd2bbadac41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Sat, 21 Aug 2021 22:47:38 -0700 Subject: [PATCH] feat(error): diagnostic-ify MietteError --- src/error.rs | 21 ++++++++++++++++++--- src/printer/mod.rs | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index 2ebafff1..6144a9d6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,15 +2,30 @@ use std::io; use thiserror::Error; +use crate::{self as miette, Diagnostic}; + /** Error enum for miette. Used by certain operations in the protocol. */ -#[derive(Debug, Error)] +#[derive(Debug, Diagnostic, Error)] pub enum MietteError { + /// Wrapper around [std::io::Error]. This is returned when something went + /// wrong while reading a [crate::Source]. #[error(transparent)] + #[diagnostic(code(miette::io_error))] IoError(#[from] io::Error), + + /// Returned when a [crate::SourceSpan] extends beyond the bounds of a given [crate::Source]. #[error("The given offset is outside the bounds of its Source")] + #[diagnostic( + code(miette::span_out_of_bounds), + help("Double-check your spans. Do you have an off-by-one error?") + )] OutOfBounds, - #[error("Failed to install reporter hook")] - ReporterInstallFailed, + + /// Returned when installing a [crate::DiagnosticReportPrinter] failed. + /// Typically, this will be because [crate::set_printer] was called twice. + #[error("Failed to install DiagnosticReportPrinter")] + #[diagnostic(code(miette::set_printer_failed))] + SetPrinterFailure, } diff --git a/src/printer/mod.rs b/src/printer/mod.rs index 38694ca9..b8d54153 100644 --- a/src/printer/mod.rs +++ b/src/printer/mod.rs @@ -28,7 +28,7 @@ pub fn set_reporter( ) -> Result<(), MietteError> { REPORTER .set(Box::new(reporter)) - .map_err(|_| MietteError::ReporterInstallFailed) + .map_err(|_| MietteError::SetPrinterFailure) } /// Used by [DiagnosticReport] to fetch the reporter that will be used to