pub struct Reporter { /* private fields */ }Expand description
The error reporter of one request, job batch or cron run: Ctx::errors.
Cheap to clone; clones share their context and pending reports.
Reporter::default() builds one for unit tests, whose reports are read
back with take.
§Examples
use ocre::errors::{Reporter, Severity};
let reporter = Reporter::default();
let value: Option<i32> = reporter.handle("x".parse::<i32>());
assert_eq!(value, None);
let reports = reporter.take();
assert_eq!((reports[0].handled, reports[0].severity), (true, Severity::Warning));Implementations§
Source§impl Reporter
impl Reporter
Sourcepub fn set_context(&self, key: &str, value: impl Serialize)
pub fn set_context(&self, key: &str, value: impl Serialize)
Adds context to every later report of this request or job (Rails’ Rails.error.set_context).
§Examples
let reporter = ocre::errors::Reporter::default();
reporter.set_context("user_id", 7);
reporter.report(&"oops", ocre::errors::Options::new());
assert_eq!(reporter.take()[0].context["user_id"], 7);Sourcepub fn report<E: Display + ?Sized>(&self, error: &E, options: Options)
pub fn report<E: Display + ?Sized>(&self, error: &E, options: Options)
Reports an error (Rails’ Rails.error.report): logs it and queues it for the subscribers.
§Examples
let reporter = ocre::errors::Reporter::default();
let err = ocre::Error::internal("webhook signature mismatch");
reporter.report(&err, ocre::errors::Options::new().source("webhooks"));
assert_eq!(reporter.take()[0].message, "internal error: webhook signature mismatch");Sourcepub fn handle<T, E: Display>(&self, result: Result<T, E>) -> Option<T>
pub fn handle<T, E: Display>(&self, result: Result<T, E>) -> Option<T>
Reports the error of result, if any, as handled, and returns its value (Rails’ Rails.error.handle).
.unwrap_or(fallback) gives Rails’ fallback:. Only errors of the
result’s type are caught: other errors propagate with ? before
reaching it (Rails’ class filter).
§Examples
let reporter = ocre::errors::Reporter::default();
assert_eq!(reporter.handle("4".parse::<u8>()), Some(4));
assert_eq!(reporter.handle("x".parse::<u8>()).unwrap_or(1), 1);
assert_eq!(reporter.take().len(), 1);Sourcepub fn record<T, E: Display>(&self, result: Result<T, E>) -> Result<T, E>
pub fn record<T, E: Display>(&self, result: Result<T, E>) -> Result<T, E>
Reports the error of result, if any, as unhandled, and returns result unchanged (Rails’ Rails.error.record).
§Examples
use ocre::errors::{Reporter, Severity};
let reporter = Reporter::default();
assert!(reporter.record("x".parse::<u8>()).is_err());
let report = &reporter.take()[0];
assert_eq!((report.handled, report.severity), (false, Severity::Error));Sourcepub fn unexpected(&self, message: impl Display)
pub fn unexpected(&self, message: impl Display)
Reports something that should never happen (Rails’ Rails.error.unexpected).
Panics in debug builds (ocre dev, tests), so the bug is seen at
once; in release builds it is reported as an error and the code goes on.
§Panics
In debug builds, always.
§Examples
ocre::errors::Reporter::default().unexpected("a paid order has no invoice");