Is tracing intended to be used as I am using it? How can I trace errors chain? #2329
-
I'm using async-graphql and axum. This is a reproduction of the issue: /~https://github.com/frederikhors/iss-async-graphql-error-handling. To start:
If you open the GraphiQL client at http://localhost:8000, you can use the below query to simulate what I'm trying to understand: mutation {
mutateWithError
} The backend response is: {
"data": null,
"errors": [
{
"message": "I cannot mutate now, sorry!",
"locations": [
/*...*/
],
"path": ["mutateWithError"]
}
]
} I like this, but what I don't understand is the tracing part:
Do you see the
CodeThe code is very simple: pub struct Mutation;
#[Object]
impl Mutation {
async fn mutate_with_error(&self) -> async_graphql::Result<String> {
let new_string = mutate_with_error().await?;
Ok(new_string)
}
}
async fn mutate_with_error() -> anyhow::Result<String> {
match can_i_mutate_on_db().await {
Ok(s) => Ok(s),
Err(err) => Err(err.context("I cannot mutate now, sorry!")),
}
}
async fn can_i_mutate_on_db() -> anyhow::Result<String> {
bail!("this is a DB error!")
}
async fn graphql_handler(
schema: Extension<Schema<Query, Mutation, EmptySubscription>>,
req: GraphQLRequest,
) -> GraphQLResponse {
schema.execute(req.into_inner()).await.into()
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
I'm not familiar with the Similarly, the reason the error = %err.message(), when emitting the If the error type in this code implements To summarize, it's possible to record errors the way you want using tracinglib::error!(target: "async_graphql::graphql",
error = &err
"error"); or, if the compiler doesn't like that, you may need to explicitly cast to tracinglib::error!(target: "async_graphql::graphql",
error = &err as &(dyn std::error::Error + 'static)
"error"); If the maintainers aren't interested in accepting a PR to change how the event is recorded, you could probably use a modified version of their |
Beta Was this translation helpful? Give feedback.
I'm not familiar with the
async-graphql
library, but it looks like the tracing event is being emitted by that library in this case. The event is at theINFO
level rather than theERROR
level because that library's authors chose to record the event atINFO
: /~https://github.com/async-graphql/async-graphql/blob/ad05baf79aea55a0827291abf55af5ce7cc8d386/src/extensions/tracing.rs#L146-L148Similarly, the reason the
tracing
event includes only the top-level error message is because theasync-graphql
authors chose to writewhen emitting the
tracing
event I linked above. That code is saying "record a field namederror
whose value iserr.message()
, formatted usingfmt::Display
.