Skip to content

Errors

Routes, constructors, middlewares: they can fail.

#[pavex::post(path = "/login")]
pub fn login(head: &RequestHead) -> Result<Response, LoginError /* (1)! */> {
    // [...]
}
  1. The request handler is fallible because it returns a Result, with LoginError as its error type.

What happens on failure? What should the framework do with the error?
Two different concerns must be addressed:

  • Reacting: whoever called your API is waiting for a response! The error must be converted into an HTTP response.
  • Reporting: you need to know when something goes wrong—and why.
    You must be able to report that an error occurred using your preferred monitoring system (e.g. a log record, incrementing a counter, sending a notification, etc.).

These concerns are addressed by two different kinds of Pavex components: error handlers and error observers.

Note

Check out this article for a deep dive on the topic of error handling (in Rust and beyond).