juniper
juniper copied to clipboard
Give `IntoFieldError::into_field_error` access to `Context`
In the app I'm working on we want to track all errors in Rollbar. The way we're currently doing that is by having a custom error type (AppError) and implementing IntoFieldError for that type. In into_field_error we then make an API call to Rollbar and return a FieldError.
This works well however we would like for the data we send to Rollbar to contain meta data such as the current user's id and the operation name(s). This is a bit problematic because IntoFieldError::into_field_error doesn't have anyway of getting to the Context, which is where we store these things.
We solve that by having a type that wraps a normal error and has the data from the Context we need. Something like:
pub struct ReportableError {
error: AppError,
current_user_id: Option<i32>,
operation_names: Vec<String>,
}
We then have some setup for creating those errors from a Result<_, AppError> and the Executor. Something like:
let result: Result<_, ReportableError> = some_result_value
.map_err_to_reportable(executor);
This does work but I'm thinking we could simplify things quite a bit if IntoFieldError::into_field_error got the Context or Executor as an argument. That way I imagine we wouldn't need a wrapper type and the conversion should happen automatically.
What do you think?
I'm guessing this would be a breaking change.
I wouldn't mind giving this a shot if you think its a good idea, if not that is cool too 😊
We definitely want a way to do this, but this is closely correlated with things like general logging and tracing.
My preferred solution to this is moving towards a stateful Executor, which makes adding things like error/reporting/tracing hooks easier.
Fair enough. I'll close the PR.