Skip to main content

Reporter

Struct Reporter 

Source
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

Source

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);
Source

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");
Source

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);
Source

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));
Source

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");
Source

pub fn take(&self) -> Vec<Report>

Takes the reports made so far, for the subscribers (or a test).

§Examples
let reporter = ocre::errors::Reporter::default();
assert!(reporter.take().is_empty());

Trait Implementations§

Source§

impl Clone for Reporter

Source§

fn clone(&self) -> Reporter

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Reporter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Reporter

Source§

fn default() -> Reporter

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V