#trillium

macro trillium-error

Error handling support for trillium.rs

1 unstable release

0.1.0 Oct 11, 2022

#41 in #trillium

MIT/Apache

8KB
52 lines

trillium-error

This crate adds support for error handling in trillium web framework.

Due to limitations in Rust, error handling is currently not supported in trillium. When the language adds capability to express bounds for for<'a> Fn(&'a Conn) -> impl Future<Output=> + 'a, trillium will add first class support for error handling. For more details please refer to the discussion here. Until then trillium-error provides a proc macro to help write handlers with error.

Usage

Refer to the docs.

LICENSE

License under Apache 2.0 or MIT


lib.rs:

This crate adds support for error handling in trillium web framework.

Due to limitations in Rust, error handling is currently not supported in trillium. When the language adds capability to express bounds for for<'a> Fn(&'a Conn) -> impl Future<Output=> + 'a, trillium will add first class support for error handling. Until then trillium-error provides a proc macro to help write handlers with error. For more details please refer to the discussion here.

use trillium_error::handler;

#[derive(thiserror::Error, Debug)]
pub enum AppError {
#[error("Custom error")]
CustomError,
#[error("IO error")]
IoError(std::io::Error),
}

impl From<std::io::Error> for AppError {
fn from(err: std::io::Error) -> Self {
AppError::IoError(err)
}
}

#[async_trait]
impl Handler for AppError {
async fn run(&self, conn: Conn) -> Conn {
conn.with_status(500).with_body("Internal Server Error")
}
}

#[handler]
async fn helloworld(conn: &mut Conn) -> Result<(), AppError> {
conn.set_status(200);
conn.set_body("hello world");
// Ok(())
Err(AppError::CustomError)
}

fn main() {
trillium_tokio::run(helloworld);
}

Dependencies

~1.5MB
~37K SLoC