-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for anyhow::Error #1241
Conversation
I am not planing to support it. and |
Codecov Report
@@ Coverage Diff @@
## master #1241 +/- ##
=======================================
Coverage 80.45% 80.45%
=======================================
Files 159 159
Lines 18518 18518
=======================================
Hits 14899 14899
Misses 3619 3619
Continue to review full report at Codecov.
|
Hello, may I ask why is |
Hmm. I could not find any mentions of the |
Apologies! I couldn't find it now, though it must have been there quite recently, as I haven't been working with actix for more than a few weeks, and I clearly remember learning about In that case, may I instead ask why was automatic error compatibility abandoned completely? A generic, simple way to return a 500 on the off-chance a rare error occurs is something I'd wager to be common in most projects utilizing |
Well, actix provides a bunch of helper functions to generate required errors from any type that implements Isn't it flexible enough for your needs? |
Well, it's flexible enough, but it turns every #[get("/get/{object_id}")]
pub async fn get_object(
state: web::Data<State>,
address_str: web::Path<String>,
) -> Result<HttpResponse, actix_web::Error> {
let response: Result<Vec<Entry>> = state
.db
.send(crate::database::RetrieveObject {
target: Address::decode(
&decode(address_str.into_inner()).map_err(ErrorInternalServerError)?,
)
.map_err(ErrorInternalServerError)?,
})
.await.map_err(ErrorInternalServerError)?;
debug!("{:?}", response);
let entries = response.map_err(error::ErrorInternalServerError)?;
let mut result: HashMap<String, serde_json::Value> = HashMap::new();
for entry in entries {
result.insert(
encode(entry.identity).map_err(ErrorInternalServerError)?,
json!({
"target": encode(entry.target.encode().map_err(ErrorInternalServerError)?).map_err(ErrorInternalServerError)?,
"key": entry.key,
"value": entry.value.as_string().map_err(ErrorInternalServerError)?,
}),
);
}
Ok(HttpResponse::Ok().json(result))
} But once again, I might be overlooking something? edit: For instance, my code being overly reliant on |
Don't worry, you're not going mad. It was updated as part of this PR which was merged yesterday: actix/actix-website#188 We'd like to encourage use of the standard Error trait rather than tying the project into specific error handling crates. A review of error handling might be something we can look at soon though. In my own apps, I feel the easiest and cleanest method is a global error enum that implements ResponseError (with custom status code and responses for each variant) and has From impls for each error type you want to just use ? on. |
anyhow is an error handling crate like failure. It seems to be better supported and more popular now as it uses std::error::Error and not a custom trait. This PR adds the same sort of support failure has, also behind a feature flag.