Skip to main content

Module hook

Module hook 

Source
Expand description

before and after — one interception point for every operation.

A hook runs around an operation without the operation knowing. That is what authorization, audit, metrics and tracing all want, and building them on one mechanism is why this module exists: four separate interception points would be four places to forget, four orderings to reason about, and four things to test.

use ash_jsonapi::hook::{Event, Hook, Outcome};
use ash_jsonapi::{Context, JsonApiError};

struct Timing;

impl Hook for Timing {
    fn after(&self, event: &Event<'_>, outcome: &Outcome, _ctx: &Context) {
        println!(
            "{} {} -> {}",
            event.operation.as_str(),
            event.resource,
            outcome.status()
        );
    }
}

§What a before hook can do

Return an error, and the operation does not run: the request is answered with what the hook returned. That makes before the seam for authorization — a denial should never reach storage — and for anything else that decides a request must not proceed.

An after hook cannot change the response. It observes: it is told what happened, and its return value is nothing. Recording must not be able to break a request that otherwise succeeded, and a hook that could rewrite a response is a second, invisible handler.

§Ordering

before hooks run in registration order, and the first error stops the rest — so a hook registered later can rely on an earlier one having passed. after hooks run in the same order, and all of them run: one panicking or misbehaving must not silence the others’ records.

Structs§

Event
What is about to run, or has just run.
Hooks
The hooks a service runs, in order.

Enums§

Operation
Which operation is running.
Outcome
How an operation finished.

Traits§

Hook
Something that runs around every operation.