GithubHelp home page GithubHelp logo

anyhow_ext's Introduction

anyhow_ext

An extension of anyhow. A drop-in replacement of anyhow.

Features

  • Attach file location info in error message
  • Use dot to make a small backtrace.

usage

Work with anyhow!

[dependencies]
anyhow = "1"
anyhow_ext = "0.2"

Then use anyhow_ext::Context instead of anyhow::Context.

use anyhow::{anyhow, Result};
use anyhow_ext::Context;

fn foo() -> Result<()> {
    Err(anyhow!("an anyhow err")).context("wrap with file info")?;
    Ok(())
}

drop-in replacement of anyhow

Since anyhow_ext re-exports all the thing in anyhow except for Context, you can use anyhow_ext as a drop-in replacement.

Cargo.toml

[dependencies]
anyhow_ext = "0.2"

Then

use anyhow_ext::{Result,anyhow,Context};

fn foo() -> Result<()> {
    Err(anyhow!("an anyhow err")).context("wrap with file info")?;
    Ok(())
}

How localtion info is displayed

println!("{}", err) looks like

foo err at `src/bin/anyhow_ext.rs@6:11`

and println!("{:?}", err) looks like

  foo err at `src/bin/anyhow_ext.rs@6:11`

  Caused by:
  0: read_a_file err at `src/bin/anyhow_ext.rs@10:19`
  1: an io err at `src/bin/anyhow_ext.rs@15:55`
  2: No such file or directory (os error 2)

dot to make a small backtrace

use anyhow_ext::{Context, Result};

fn foo() -> Result<()> {
    std::fs::read_to_string("file_not_exists.txt").dot()?;
    Ok(())
}

fn bar() -> Result<()> {
    foo().dot()?;
    Ok(())
}

fn main() {
    if let Err(err) = try_main().dot() {
        println!("{:?}", err);
    }
}

fn try_main() -> Result<()> {
    bar().dot()?;
    Ok(())
}

This will make a small backtrace.

at `examples/dot.rs@14:34`

Caused by:
    0: at `examples/dot.rs@20:11`
    1: at `examples/dot.rs@9:11`
    2: at `examples/dot.rs@4:52`
    3: No such file or directory (os error 2)

anyhow_ext's People

Contributors

gensmusic avatar x0f5c3 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.