GithubHelp home page GithubHelp logo

Comments (7)

mjc-gh avatar mjc-gh commented on August 29, 2024 5

I wrote a small macro to assert against the error types that are returned by my library. Not sure if this will be useful to anyone else but I figured I would share 😃

// usage:
//    assert_error_kind!(some_err, ErrorKind::MyErrorType)
macro_rules! assert_error_kind {
    ($err:expr, $kind:pat) => (match *$err.kind() {
        $kind => assert!(true, "{:?} is of kind {:?}", $err, stringify!($kind)),
        _     => assert!(false, "{:?} is NOT of kind {:?}", $err, stringify!($kind))
    });
}

from error-chain.

colin-kiegel avatar colin-kiegel commented on August 29, 2024

I guess a #[derive(PartialEq)] should do the trick here: https://github.com/brson/error-chain/blob/62b3c0d1014d4a5b9af142be481f1eaf2b443c3d/src/error_chain.rs#L196-L197

from error-chain.

Yamakaky avatar Yamakaky commented on August 29, 2024

That would be a good idea, but that's not practical. io::Error, a very common error type, doesn't implement Eq, so auto-deriving would generate a compile time error which is not fixable by the end-user.
Since ErrorKind is generated, you can always manually implement Eq. Not ideal, but it works.

from error-chain.

sourcefrog avatar sourcefrog commented on August 29, 2024

Is there a particular recommended way to check the .kind() or should I use match?

from error-chain.

Yamakaky avatar Yamakaky commented on August 29, 2024

I think a match over ErrorKind is fine. If you also want to implement Eq on Error, you may want to pay attention to the backtrace and the error chain.

from error-chain.

sourcefrog avatar sourcefrog commented on August 29, 2024

ok thanks

from error-chain.

Yamakaky avatar Yamakaky commented on August 29, 2024

With https://github.com/Arnavion/derive-error-chain/ we may be able to derive Eq if possible.

from error-chain.

Related Issues (20)

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.