Fancy extension for std::error::Error with pretty, detailed diagnostic printing.
Go to file
Kat Marchán fe3abdc64d
docs: add narratable printouts to README
2021-08-21 15:32:50 -07:00
.github/workflows meta: set up CI (#21) 2021-08-17 23:56:12 -07:00
images docs: update readme with new stuff 2021-08-21 00:11:40 -07:00
miette-derive 0.12.0 2021-08-21 00:20:16 -07:00
src feat(printer): added (and hooked up) an accessible report printer 2021-08-21 15:32:31 -07:00
tests feat(printer): added (and hooked up) an accessible report printer 2021-08-21 15:32:31 -07:00
.clog.toml docs: update changelog 2021-08-04 21:25:25 -07:00
.editorconfig initial commit 2021-08-02 19:36:02 -07:00
.gitignore initial commit 2021-08-02 19:36:02 -07:00
CHANGELOG.md docs: update changelog 2021-08-21 00:18:02 -07:00
Cargo.toml feat(printer): added (and hooked up) an accessible report printer 2021-08-21 15:32:31 -07:00
LICENSE initial commit 2021-08-02 19:36:02 -07:00
README.md docs: add narratable printouts to README 2021-08-21 15:32:50 -07:00

README.md

you run miette? You run her code like the software? Oh. Oh! Error code for coder! Error code for One Thousand Lines!

About

miette is a diagnostic library for Rust. It includes a series of protocols that allow you to hook into its error reporting facilities, and even write your own error reports! It lets you define error types that can print out like this (or in any format you like!):

Hi! miette also includes a screen-reader-oriented diagnostic printer that's enabled in various situations, such as when you use NO_COLOR or CLICOLOR settings, or on CI. This behavior is also fully configurable and customizable. For example, this is what this particular diagnostic will look like when the narrated printer is enabled:
\
Error: Received some bad JSON from the source. Unable to parse.
Caused by: missing field `foo` at line 1 column 1700
\
Begin snippet for https://api.nuget.org/v3/registration5-gz-semver2/json.net/index.json starting
at line 1, column 1659
\
snippet line 1: gs":["json"],"title":"","version":"1.0.0"},"packageContent":"https://api.nuget.o
highlight starting at line 1, column 1699: last parsing location
\
diagnostic help: This is a bug. It might be in ruget, or it might be in the source you're using,
but it's definitely a bug and should be reported.
diagnostic error code: ruget::api::bad_json

The [Diagnostic] trait in miette is an extension of std::error::Error that adds various facilities like [Severity], error codes that could be looked up by users, and snippet display with support for multiline reports, arbitrary [Source]s, and pretty printing.

miette also includes a (lightweight) anyhow/eyre-style [DiagnosticReport] type which can be returned from application-internal functions to make the ? experience nicer. It's extra easy to use when using [DiagnosticResult]!

While the miette crate bundles some baseline implementations for [Source] and [DiagnosticReportPrinter], it's intended to define a protocol that other crates can build on top of to provide rich error reporting, and encourage an ecosystem that leans on this extra metadata to provide it for others in a way that's compatible with [std::error::Error].

Installing

Using cargo-edit:

$ cargo add miette

Example and Guide

/*
You can derive a Diagnostic from any `std::error::Error` type.

`thiserror` is a great way to define them, and plays nicely with `miette`!
*/
use miette::{Diagnostic, SourceSpan};
use thiserror::Error;

#[derive(Error, Debug, Diagnostic)]
#[error("oops!")]
#[diagnostic(
    code(oops::my::bad),
    help("try doing it better next time?"),
)]
struct MyBad {
    // The Source that we're gonna be printing snippets out of.
    src: String,
    // Snippets and highlights can be included in the diagnostic!
    #[snippet(src, "This is the part that broke")]
    snip: SourceSpan,
    #[highlight(snip)]
    bad_bit: SourceSpan,
}

/*
Now let's define a function!

Use this DiagnosticResult type (or its expanded version) as the return type
throughout your app (but NOT your libraries! Those should always return concrete
types!).
*/
use miette::DiagnosticResult as Result;
fn this_fails() -> Result<()> {
    // You can use plain strings as a `Source`, or anything that implements
    // the one-method `Source` trait.
    let src = "source\n  text\n    here".to_string();
    let len = src.len();

    Err(MyBad {
        src,
        snip: ("bad_file.rs", 0, len).into(),
        bad_bit: ("this bit here", 9, 4).into(),
    })?;

    Ok(())
}

/*
Now to get everything printed nicely, just return a Result<(), DiagnosticReport>
and you're all set!

Note: You can swap out the default reporter for a custom one using `miette::set_reporter()`
*/
fn pretend_this_is_main() -> Result<()> {
    // kaboom~
    this_fails()?;

    Ok(())
}

And this is the output you'll get if you run this program:


Narratable printout:
\
Error: oops!
Diagnostic severity: error
\
Begin snippet for bad_file.rs starting at line 1, column 1
\
snippet line 1: source
snippet line 2:   text
highlight starting at line 2, column 3: these two lines
snippet line 3:     here
\
diagnostic help: try doing it better next time?
diagnostic error code: oops::my::bad

License

miette is released to the Rust community under the Apache license 2.0.

It also includes some code taken from eyre, and some from thiserror, also under the Apache License. Some code is taken from ariadne, which is MIT licensed.