From 5725f4b40b1c612d2059789cb95222ff3b0bf510 Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Sat, 8 Oct 2022 04:33:07 +0100 Subject: add some error messages (#510) * add some error messages * fmt --- atuin-server/src/handlers/mod.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'atuin-server/src/handlers/mod.rs') diff --git a/atuin-server/src/handlers/mod.rs b/atuin-server/src/handlers/mod.rs index 4aa7423e..4be3e388 100644 --- a/atuin-server/src/handlers/mod.rs +++ b/atuin-server/src/handlers/mod.rs @@ -1,5 +1,4 @@ -use std::borrow::Cow; - +use atuin_common::api::ErrorResponse; use axum::{response::IntoResponse, Json}; use serde::{Deserialize, Serialize}; @@ -23,11 +22,6 @@ pub async fn index() -> Json { }) } -#[derive(Debug, Serialize, Deserialize)] -pub struct ErrorResponse<'a> { - pub reason: Cow<'a, str>, -} - impl<'a> IntoResponse for ErrorResponseStatus<'a> { fn into_response(self) -> axum::response::Response { (self.status, Json(self.error)).into_response() @@ -39,15 +33,20 @@ pub struct ErrorResponseStatus<'a> { pub status: http::StatusCode, } -impl<'a> ErrorResponse<'a> { - pub fn with_status(self, status: http::StatusCode) -> ErrorResponseStatus<'a> { +pub trait RespExt<'a> { + fn with_status(self, status: http::StatusCode) -> ErrorResponseStatus<'a>; + fn reply(reason: &'a str) -> Self; +} + +impl<'a> RespExt<'a> for ErrorResponse<'a> { + fn with_status(self, status: http::StatusCode) -> ErrorResponseStatus<'a> { ErrorResponseStatus { error: self, status, } } - pub fn reply(reason: &'a str) -> ErrorResponse { + fn reply(reason: &'a str) -> ErrorResponse { Self { reason: reason.into(), } -- cgit v1.3.1