Giter VIP home page Giter VIP logo

extension-eyre's Introduction

extension-eyre

Re-export of color-eyre that introduces Extensions (type-map) to eyre::Report.

This is a simple wrapper around color_eyre with everything equal, and just introduces traits ExtensionExt, Extension for adding and accessing custom data.

To learn more about color-eyre, see their documentation.

TLDR

extension_eyre helps you add extensions of any type to your error like this:

custom section example

Setup

Add the following to your toml file:

[dependencies]
extension-eyre = "0.1"

And install the panic and error report handlers:

use extension_eyre::eyre::Result;

fn main() -> Result<()> {
    extension_eyre::install()?;

    // ...
    # Ok(())
}

Features

For all the features introduced by color-eyre, please see their documentation.

Adding extensions of any type to error reports via [ExtensionExt] trait

The crate exposes ExtensionExt trait for adding and Extension trait for accessing extra data to error reports. The extensions implementation is a copy of http crate's Extensions implementation.

ExtensionExt allows adding data of any type to error reports. At the application level, you can access this data with trait Extension with method report.extension_ref::<T>().

use extension_eyre::{eyre::eyre, ExtensionExt, Extension, eyre::Report};
use std::process::Command;
use tracing::instrument;

pub struct Retry;

#[instrument]
fn app(path: &str) -> Result<String, Report> {
    if let Err(err) = read_file("fake_file") {
        if let Some(Retry) = err.extension_ref() {
            // ...handle retry case
        }
    }

    Ok(Default::default())
}

#[instrument]
fn read_file(path: &str) -> Result<String, Report> {
    Command::new("cat").arg(path).output2()
}

trait Output {
    fn output2(&mut self) -> Result<String, Report>;
}

impl Output for Command {
    #[instrument]
    fn output2(&mut self) -> Result<String, Report> {
        let output = self.output()?;

        let stdout = String::from_utf8_lossy(&output.stdout);

        if !output.status.success() {
            Err(eyre!("cmd exited with non-zero status code")).extension(Retry)
        } else {
            Ok(stdout.into())
        }
    }
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

extension-eyre's People

Contributors

poonesnerfect avatar

Stargazers

Jeff Carpenter avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.