juniper icon indicating copy to clipboard operation
juniper copied to clipboard

Give `IntoFieldError::into_field_error` access to `Context`

Open davidpdrsn opened this issue 6 years ago • 3 comments

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.

davidpdrsn avatar Jul 12 '19 12:07 davidpdrsn

I wouldn't mind giving this a shot if you think its a good idea, if not that is cool too 😊

davidpdrsn avatar Jul 18 '19 09:07 davidpdrsn

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.

theduke avatar Jul 24 '19 23:07 theduke

Fair enough. I'll close the PR.

davidpdrsn avatar Jul 25 '19 06:07 davidpdrsn