11use crate :: queries:: errors:: QueryError ;
22use anyhow:: Error as AnyhowError ;
33use caryatid_module_rest_server:: messages:: RESTResponse ;
4- use std :: fmt ;
4+ use thiserror :: Error ;
55
6- /// Standard error types for the application
7- #[ derive( Debug ) ]
6+ /// Standard REST error types
7+ #[ derive( Debug , Error ) ]
88pub enum RESTError {
9+ #[ error( "{0}" ) ]
910 BadRequest ( String ) ,
11+
12+ #[ error( "{0}" ) ]
1013 NotFound ( String ) ,
14+
15+ #[ error( "{0}" ) ]
1116 InternalServerError ( String ) ,
17+
18+ #[ error( "{0}" ) ]
1219 NotImplemented ( String ) ,
1320}
1421
@@ -24,12 +31,12 @@ impl RESTError {
2431 }
2532
2633 /// Get the error message
27- pub fn message ( & self ) -> String {
34+ pub fn message ( & self ) -> & str {
2835 match self {
29- RESTError :: BadRequest ( msg) => msg. clone ( ) ,
30- RESTError :: NotFound ( msg) => msg. clone ( ) ,
31- RESTError :: InternalServerError ( msg) => msg. clone ( ) ,
32- RESTError :: NotImplemented ( msg) => msg. clone ( ) ,
36+ RESTError :: BadRequest ( msg) => msg,
37+ RESTError :: NotFound ( msg) => msg,
38+ RESTError :: InternalServerError ( msg) => msg,
39+ RESTError :: NotImplemented ( msg) => msg,
3340 }
3441 }
3542
@@ -79,18 +86,10 @@ impl RESTError {
7986 }
8087}
8188
82- impl fmt:: Display for RESTError {
83- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
84- write ! ( f, "{}" , self . message( ) )
85- }
86- }
87-
88- impl std:: error:: Error for RESTError { }
89-
9089/// Convert RESTError to RESTResponse
9190impl From < RESTError > for RESTResponse {
9291 fn from ( error : RESTError ) -> Self {
93- RESTResponse :: with_text ( error. status_code ( ) , & error. message ( ) )
92+ RESTResponse :: with_text ( error. status_code ( ) , error. message ( ) )
9493 }
9594}
9695
@@ -101,13 +100,6 @@ impl From<AnyhowError> for RESTError {
101100 }
102101}
103102
104- // /// Convert ParseIntError to RESTError (400 Bad Request)
105- // impl From<ParseIntError> for RESTError {
106- // fn from(error: ParseIntError) -> Self {
107- // RESTError::BadRequest(error.to_string())
108- // }
109- // }
110-
111103/// Convert hex decode errors to RESTError (400 Bad Request)
112104impl From < hex:: FromHexError > for RESTError {
113105 fn from ( error : hex:: FromHexError ) -> Self {
0 commit comments