From 849a348503ca38c171276f658f1ac4bd273ef2d1 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Tue, 30 Jan 2024 16:48:26 +0000 Subject: catch all errors to custom template --- src/main.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index c38b2eb..25bc21f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,6 @@ use std::rc::Rc; use std::{collections::HashSet, time::Duration}; use poem::http::StatusCode; -use poem::Response; use poem::{ endpoint::EmbeddedFilesEndpoint, get, handler, @@ -21,6 +20,7 @@ use poem::{ web::{Data, Path, Query}, EndpointExt, Route, Server, }; +use poem::{IntoResponse, Response}; use rust_embed::RustEmbed; use error::BlossomError; @@ -114,6 +114,14 @@ async fn plants() -> Result<()> { Err(BlossomError::Unimplemented) } +async fn custom_error(err: poem::Error) -> impl IntoResponse { + templates::Error { + status: err.status(), + message: err.to_string(), + } + .with_status(err.status()) +} + #[tokio::main] async fn main() -> std::result::Result<(), std::io::Error> { // let mut skinny_data = mastodon_async::Data::default(); @@ -126,6 +134,7 @@ async fn main() -> std::result::Result<(), std::io::Error> { .at("/contact", get(contact)) .at("/plants", get(plants)) .nest("/static/", EmbeddedFilesEndpoint::::new()) + .catch_all_error(custom_error) .with(AddData::new( reqwest::Client::builder() .connect_timeout(Duration::from_secs(1)) -- cgit