Skip to main content

Module errors

Module errors 

Source
Expand description

Error reporting (Rails’ Rails.error): report errors with context to the logs and to services such as Sentry.

Every Ctx has a Reporter, Ctx::errors. A report is logged at once (as an error, warn or info line with its context) and handed to each Subscriber the app registered with subscribe. Ocre reports on its own, with handled: false:

SourceWhen
ocre.requesta handler returned Error::Internal (a 500)
ocre.joba background job failed (it is retried or discarded)
ocre.crona scheduled task failed
ocre.mailboxthe inbound email handler failed
use axum::extract::State;
use ocre::{Ctx, Result, errors::{Options, Severity}};

async fn sync_orders(State(ctx): State<Ctx>) -> Result<&'static str> {
    ctx.errors().set_context("tenant", "acme"); // added to every report of this request

    // Rails.error.handle: report and go on with a fallback.
    let rates: Vec<f64> = ctx.errors().handle(fetch_rates().await).unwrap_or_default();

    // Rails.error.record: report, then fail the request.
    ctx.errors().record(charge().await)?;

    // Rails.error.report, with options.
    if rates.is_empty() {
        let options = Options::new().severity(Severity::Info).context("provider", "ecb").source("rates");
        ctx.errors().report(&"no exchange rates today", options);
    }
    Ok("OK")
}

async fn fetch_rates() -> Result<Vec<f64>> {
    Ok(vec![1.08])
}
async fn charge() -> Result<()> {
    Ok(())
}

§Subscribers

Register them once per Worker instance, in the start event of src/lib.rs (Ocre’s initializer):

#[worker::event(start)]
fn start() {
    ocre::errors::subscribe(ocre::errors::Sentry);
}

Sentry sends each report to the project of the SENTRY_DSN secret (sentry.io or any Sentry-compatible service, such as GlitchTip or Bugsink). Other services implement Subscriber: it turns a Report into the HTTP request to send (Delivery), and Ocre sends it with fetch once the response is ready.

§Free plan

Reports cost nothing until a subscriber sends one: then each delivery is one subrequest (50 per request on the free plan), made after the handler, before the response goes out, so an error response waits for it. Only reports that happen are sent; a request without errors makes no subrequest.

Structs§

Delivery
An HTTP POST a Subscriber asks Ocre to send for a report.
Options
Options of one report (Rails’ handled:, severity:, context:, source:).
Report
One reported error, as subscribers receive it.
Reporter
The error reporter of one request, job batch or cron run: Ctx::errors.
Sentry
Sends reports to Sentry or a Sentry-compatible service, as envelopes (the Sentry ingestion protocol).

Enums§

Severity
How bad a report is (Rails’ severity:).

Constants§

SENTRY_DSN
Worker secret holding the Sentry DSN: https://<key>@<host>/<project id>.

Traits§

Subscriber
An error-reporting service (Rails’ error subscribers): turns a Report into the request to send.

Functions§

subscribe
Registers a subscriber for every report of this Worker instance (Rails’ Rails.error.subscribe).