GithubHelp home page GithubHelp logo

Use own error type for API about calloop HOT 5 CLOSED

smithay avatar smithay commented on July 20, 2024
Use own error type for API

from calloop.

Comments (5)

elinorbgr avatar elinorbgr commented on July 20, 2024

Yes, definitely. Notably, process_events() should return a result that indicates, when an error occurred, if it came from the polling machinery itself or from one of the event sources, in which case it could be a Box<dyn Error>. Probably worth thinking to some mechanism that allows to easily detect which source errored, to act upon that if possible/necessary.

from calloop.

detly avatar detly commented on July 20, 2024

Excellent, we can discuss the general approach here and then I'll code it up.

I tend to avoid Box<dyn Error> - basically it doesn't do a whole lot that anyhow or thiserror don't achieve between them. Both of those libs will happily work with (ie. wrap, allow unwrapping, combine) errors that implement std::error::Error, so we should be fine there.

My first suggestion would be to have a calloop::CalloopError that

  • implements std::error::Error
  • has From impls from std::io::Error into itself
    • possibly has a few distinct types that correspond to obvious std::io::ErrorKinds without going overboard
  • has From impls from std::error::Error into itself
  • has enough additional information that the event source can at least be identified (access it directly might have ownership issues, because the loop owns the source, but eg. the handle's registration token or the source's token could be recorded)

Off the top of my head, we would also need a From impl for std::result::Result so that ? works (because it can't automatically do Result<T, E> into Result<S, F> just because T: Into<S> and E: Into<F>).

I think we get a lot of that for free with thiserror, so if you're not averse to adding that as a dependency, I'll use it. If not, I won't, but there might be extra boilerplate.

from calloop.

elinorbgr avatar elinorbgr commented on July 20, 2024

I'm fine with using thiserror, but without Box<dyn Error> we cannot allow the event sources to have their own custom error types. I thought that that was what you wanted?

from calloop.

detly avatar detly commented on July 20, 2024

Ah I see what you're saying. The significant part for me is not so much "event sources having custom error types" as "having error types that can easily be chained with ? or maintain a traceback through the callbacks." I'm actually not sure how much a boxed error would have to proliferate, so maybe I should code something up and see what's possible?

from calloop.

elinorbgr avatar elinorbgr commented on July 20, 2024

Ah I see. I don't know how much of a necessity having custom error types for event sources is. But I guess we can still have a fancy calloop error type based on thiserror, which a Custom(Box<dyn Error>) variant for cases where the event source really needs it.

from calloop.

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.