Coder Social home page Coder Social logo

except's Introduction

Except

The only one Error.

only available in nightly toolchain now.

Why?

The official error handling method of Rust is Result<T, E> where E: Error.

But Error is too complicated, various types need to be converted, and each crate has its own set of Error.

Even worse, enum nesting will occur, such as:

enum BazError {
    IO(std::io::Error),
}

enum BarError {
    IO(std::io::Error),
    Baz(BazError),
}

enum FooError {
    IO(std::io::Error),
    Bar(BarError),
}

How many times std::io::Error occurs here?

The anyhow::Error is good, but it is generally only used for application.

Solution

This is just a personal opinion.

An Error actually only contains the following elements:

  • type: Auto generated id, used to determine whether the Error is a certain type.
  • sub_type: Auto generated id, used to determine whether the Error is a certain sub type, used to supplement type.
  • message: String describing the Error.
  • data: Optional Error data.
  • backtrace: Error call stack.
  • source: Optional previous Error.

For Rust, the message, backtrace, source already exists in std::error::Error.

Then I prefer to auto generate the type, I think TypeId is a solution.

For data, I don't have the best idea, because it may be of any type. In order to achieve only one Error, I chose to use Box<dyn Any> internally to save it.

Example

use except::ErrorBuilder;

pub struct MyErrorKind;

pub fn foo() -> except::Result<()> {
    Err(ErrorBuilder::new::<MyErrorKind>().message("this is my error").build())
}

pub fn main() {
    if let Err(ex) = foo() {
        if ex.is::<MyErrorKind>() {
            eprintln!("my error detected: {:?}", ex);
        }
    }
}

License

Apache-2.0

except's People

Contributors

jmjoy avatar

Watchers

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