From 99bb59373b5918b2baf7e70f510d2b0ec1122a28 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 16 Sep 2024 22:40:24 +0200 Subject: Use `AppState` instead of `String` using some traits --- src/errors.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/errors.rs') diff --git a/src/errors.rs b/src/errors.rs index 1b9a802..3dda65c 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,3 +1,5 @@ +use core::fmt; + use axum::{ http::StatusCode, response::{IntoResponse, Response}, @@ -66,3 +68,31 @@ impl From for AppError { AppError::BadRequest(error.to_string()) } } + +/// Implementation of the `{}` marker for AppError +impl fmt::Display for AppError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + AppError::Database => write!(f, "Database"), + AppError::BadRequest(value) => write!(f, "BadRequest: {}", value), + AppError::NotFound(value) => write!(f, "Not found: {}", value), + AppError::TokenCreation => write!(f, "Token creation"), + AppError::InvalidToken => write!(f, "Invalid Token"), + AppError::Unauthorized => write!(f, "Unauthorized"), + } + } +} + +/// A tokio_postgres error is mapped to an `AppError::Database` +impl From for AppError { + fn from(_: tokio_postgres::Error) -> Self { + AppError::Database + } +} + +/// A async_graphql error is mapped to an `AppError::BadRequest` +impl From for AppError { + fn from(value: async_graphql::Error) -> Self { + AppError::BadRequest(value.message) + } +} -- cgit v1.2.3-18-g5258