Comments (3)
For those looking for a solution to use in the meantime, this pattern combining eyre
with thiserror
works quite well:
#[derive(thiserror::Error, Debug)]
pub enum RouteNameHereError {
#[error("{0}")]
ValidationError(String),
#[error(transparent)]
UnexpectedError(#[from] eyre::Error),
}
impl ResponseError for RouteNameHereError {
fn status_code(&self) -> StatusCode {
match self {
RouteNameHereError::ValidationError(_) => StatusCode::BAD_REQUEST,
RouteNameHereError::UnexpectedError(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
#[get("/route-name-here")]
async fn route_name_here() -> Result<(), RouteNameHereError> {
Err(eyre!("Something went wrong!"))
}
Source: https://www.lpalmieri.com/posts/error-handling-rust/#using-anyhow-as-opaque-error-type
from eyre.
Hi, great library, I'm enjoying it.
Thank you and I'm glad. Hearing that puts a smile on my face ^_^
Any tips on how I could use eyre::Result in this scenario? And food for thought, here is how I hacked some existing code to use the stdlib Result in actix-web:
The standard approach is to use a feature flag to implement the given trait in one crate or the other. It looks like someone already requested1 support for anyhow in actix-web and got denied, so having the trait implementation live in actix-web is likely out of the question, tho I suppose it would be worth double checking if the maintainer involved in that decision is still the lead maintainer or not. Edit: it seems that they are not infact involved anymore
Looking at ResponseError
it looks like it's meant to solve the same problems I'm trying to solve with generic member access, though it does so statically rather than dynamically which has a different set of tradeoffs. Either way though it wouldn't work with eyre
because of limitations in rust's trait system that currently prevent eyre
from implementing Error
, even if we could convince actix-web
to switch back to the standard library Error
trait rather than requiring their own.
In the end it looks like this is a rather large mess. I can understand why actix decided to define their own Error
trait but doing so makes them fundamentally incompatible with the rest of the ecosystem's error handling, so I can't immediately think of any good solutions for using eyre
in that environment. It might be that we can convince them to switch back to the Error
trait when generic member access is stabilized, and I would certainly love to get their feedback on that feature to know whether or not it would be an acceptable solution for their usecases, but that's only one of the problems to solve here. I'm sorry I don't have better news >_<.
Footnotes
from eyre.
It actually looks like the maintainer who closed that PR isn't the one maintaining actix-web anymore, so I think it's almost certainly worth pinging @robjtede here to get their opinion and to see if we could start working on migrating actix-web
back to the standard library error trait.
from eyre.
Related Issues (20)
- Remove obsolete private_in_public lint in nightly. HOT 1
- Avoid filename collision in the monorepo structure
- Handle future-incompat dependencies HOT 2
- 0.6.9 fails to build on unstable-less nightly HOT 14
- Extend `Option` with `ok_or_eyre`? HOT 2
- Improve discoverability of `OptionExt::ok_or_eyre` HOT 2
- Ergonomics: Hide `context` behind a feature HOT 4
- [breaking] remove alias exports `DefaultContext` and `EyreContext`
- [breaking] Remove `anyhow` from default features HOT 2
- `cargo doc` warning: "this URL is not a hyperlink"
- Segfault in `Report::downcast()` introduced in 0.6.9 HOT 6
- enable raw mode compatibility for panic & error reports HOT 1
- `fn Ok()` breaks all code with `use eyre::*` in new releases HOT 5
- `OptionExt` feature test is incorrectly feature-gated behind the `anyhow` flag
- [ BREAKING ] `ContextCompat` contains identically named methods to `WrapErr` which can be confusing and a footgun HOT 1
- [email protected] missing in the repository? HOT 2
- Eyre broken in rust-analyzer with nightly HOT 1
- and_then() that automatically converts error type to Report? HOT 11
- docs: enclosing in backticks HOT 1
- Implement From for anyhow errors
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eyre.