diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..74b7bd6 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,23 @@ +use anyhow::anyhow; +use axum::{ + http::StatusCode, + response::{IntoResponse, Response}, +}; + +pub struct InternalError(anyhow::Error); + +impl IntoResponse for InternalError { + fn into_response(self) -> Response { + ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Something went wrong: {}", self.0), + ) + .into_response() + } +} + +impl From for InternalError { + fn from(value: sqlx::Error) -> Self { + InternalError(anyhow!("Database error: {}", value)) + } +} diff --git a/src/main.rs b/src/main.rs index ce0cb16..c9242dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,9 @@ mod content; +mod error; -use anyhow::{anyhow, Result}; -use axum::{ - extract::State, - http::StatusCode, - response::{IntoResponse, Response}, - routing::get, - Router, Server, -}; +use anyhow::Result; +use axum::{extract::State, routing::get, Router, Server}; +use error::InternalError; use figment::{ providers::{Env, Format, Serialized, Toml}, Figment, @@ -33,29 +29,11 @@ impl Default for Config { } } -struct GenericError(anyhow::Error); - -impl IntoResponse for GenericError { - fn into_response(self) -> Response { - ( - StatusCode::INTERNAL_SERVER_ERROR, - format!("Something went wrong: {}", self.0), - ) - .into_response() - } -} - -impl From for GenericError { - fn from(value: sqlx::Error) -> Self { - GenericError(anyhow!("Database error: {}", value)) - } -} - struct AppState { database: Pool, } -async fn root(State(state): State>) -> Result { +async fn root(State(state): State>) -> Result { let select_query: sqlx::query::QueryAs<'_, _, Page, _> = sqlx::query_as::<_, Page>("SELECT * FROM pages"); let page: Page = select_query.fetch_one(&state.database).await?;