Skip to content

Errors

Request handlers, constructors, middlewares: they can all be fallible.

src/core/routes.rs
pub fn handler(head: &RequestHead) -> Result<Response, LoginError /* (1)! */> {
    // Handler logic...
    // [...]
}
  1. The request handler is fallible because it returns a Result, with LoginError as its error type.

What happens when they fail, though? What does 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 two concerns are addressed by two different 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).