sentry-rust icon indicating copy to clipboard operation
sentry-rust copied to clipboard

Implement a concrete SentryError type & Result Type

Open kvnvelasco opened this issue 3 years ago • 1 comments
trafficstars

I've been using the sentry types in a production app for a while now and I've noticed some internal patterns that might be useful to the broader sentry ecosystem.

The first is a concrete SentryError type

struct SentryError {
  inner: Box<dyn std::error::Error>
}

The semantics of this type are, if we hold this type we have sent an event to sentry. The type does not implement std::error::Error. It does implement Display, Debug, Send and Sync

In order to facilitate conversion to this type as well as being able to add additional metadata there is a trait

// implement this trait on your custom error types to add metadata to the sentry event when it eventually gets turned into one
trait IntoSentryEvent { 
   fn sentry_context(meta: &mut BTreeMap);

   fn into_sentry_event(self) -> SentryError {
    // default implementation goes here including dispatching the error 
  }
}

And finally

impl From<T> for SentryError where T: IntoSentryEvent { .. }

This way when I define a function boundary

fn do_something() -> SentryResult<()> {}

The signature tells me that a sentry event has been emitted at least at this boundary or somewhere internally and if errors are propagated, all errors at this boundary have been sent to sentry once. Once being the fun bit.

kvnvelasco avatar Jun 24 '22 02:06 kvnvelasco

Internally I also include a default implementation of IntoSentryEvent for anyhow::Error that way I can interface with some untyped crates.

kvnvelasco avatar Jun 24 '22 02:06 kvnvelasco