-
-
Notifications
You must be signed in to change notification settings - Fork 466
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
[FR] A new era of error handling #1094
Comments
You can do it like this: pub async fn gql_handler(req: GraphQLRequest) -> GraphQLResponse {
schema.execute(req.into_inner()).inspect_err(|err| {
let Some(source) = err.source() { // get the error source
if let Some(app_err) = source.downcast_ref::<AppError>() { // cast to AppError
// do something
}
}
}).await
} |
I'm getting this error:
|
Why do you think this is not working? |
I've tried everything but can't get it to work. Do I need to import any |
Okay, you probably need |
Thanks, I tried adding: use async_graphql::futures_util::TryFutureExt;
//..
pub async fn gql_handler(req: GraphQLRequest) -> GraphQLResponse {
schema.execute(req.into_inner())
.inspect_err() // error
.await
} the error is:
|
Yeah right, trait bound error, I should stop guessing. 🥲 |
I think something should be done on this project to enable it. But I don't know what... :( |
This compiled for me: pub async fn handle_graphql_request(req: Request<AxumBody>, schema: RootSchema) -> Result<String, Error> {
use async_graphql::futures_util::TryFutureExt;
[...]
let gql_response = schema.execute(gql_req).await.into_result();
match gql_response {
Ok(_) => {},
Err(ref errors) => {
for err in errors {
error!("Test1:{:?}", err);
if let Some(ref source) = err.source { // get the error source
error!("Test2:{:?}", source);
if let Some(ref app_err) = source.downcast_ref::<anyhow::Error>() { // cast to anyhow::Error
error!("Test3:{:?}", app_err);
}
}
}
},
}
let response_str: String = serde_json::to_string(&gql_response)?;
Ok(response_str)
} So that seems to be an alternative way of implementing the "inspect_err" usage in @sunli829's post here: #1094 (comment)
EDIT2: Actually, disregard the two paragraphs above; I think this may just be due to some modifications I was making to the errors at another stage (of logging and stripping any backtraces from errors in my |
Ok, after a lot of reading/PRs/questions/answers I think I finally have a formal request to make.
Let's say I'm using in my app a custom
AppError
like the below:I think we need:
A way to convert this custom error to
async_graphql::ServerError
:A function? A callback? Or an
impl From<AppError> for async_graphql::ServerError
?I would like it to be "transparent" to the developer: I mean NO
.map_err()
in resolvers, only the?
usage;A function/callback to call when the resolver is done with the (original) error as argument to consume it (eg.: custom tracing, API call, DB insertion etc.):
Example:
What do you think?
The text was updated successfully, but these errors were encountered: