@@ -15,7 +15,16 @@ use log::*;
15
15
pub type Result < T > = core:: result:: Result < T , Error > ;
16
16
17
17
#[ derive( Debug ) ]
18
- pub struct Error ( DomainError ) ;
18
+ pub enum Error {
19
+ Domain ( DomainError ) ,
20
+ Web ( WebErrorKind ) ,
21
+ }
22
+
23
+ #[ derive( Debug ) ]
24
+ pub enum WebErrorKind {
25
+ Input ,
26
+ Other ,
27
+ }
19
28
20
29
impl StdError for Error { }
21
30
@@ -28,41 +37,100 @@ impl std::fmt::Display for Error {
28
37
// List of possible StatusCode variants https://docs.rs/http/latest/http/status/struct.StatusCode.html#associatedconstant.UNPROCESSABLE_ENTITY
29
38
impl IntoResponse for Error {
30
39
fn into_response ( self ) -> Response {
31
- match & self . 0 . error_kind {
32
- DomainErrorKind :: Internal ( internal_error_kind) => match internal_error_kind {
33
- InternalErrorKind :: Entity ( entity_error_kind) => match entity_error_kind {
34
- EntityErrorKind :: NotFound => {
35
- warn ! (
36
- "EntityErrorKind::NotFound: Responding with 404 Not Found. Error: {:?}" ,
37
- self
38
- ) ;
39
- ( StatusCode :: NOT_FOUND , "NOT FOUND" ) . into_response ( )
40
- }
41
- EntityErrorKind :: Invalid => {
42
- warn ! ( "EntityErrorKind::Invalid: Responding with 422 Unprocessable Entity. Error: {:?}" , self ) ;
43
- ( StatusCode :: UNPROCESSABLE_ENTITY , "UNPROCESSABLE ENTITY" ) . into_response ( )
44
- }
45
- EntityErrorKind :: Other => {
46
- warn ! ( "EntityErrorKind::Other: Responding with 500 Internal Server Error. Error: {:?}" , self ) ;
47
- ( StatusCode :: INTERNAL_SERVER_ERROR , "INTERNAL SERVER ERROR" ) . into_response ( )
48
- }
49
- } ,
50
- InternalErrorKind :: Other => {
51
- warn ! ( "InternalErrorKind::Other: Responding with 500 Internal Server Error. Error: {:?}" , self ) ;
52
- ( StatusCode :: INTERNAL_SERVER_ERROR , "INTERNAL SERVER ERROR" ) . into_response ( )
53
- }
54
- } ,
55
- DomainErrorKind :: External ( external_error_kind) => {
56
- match external_error_kind {
57
- ExternalErrorKind :: Network => {
58
- warn ! ( "ExternalErrorKind::Network: Responding with 502 Bad Gateway. Error: {:?}" , self ) ;
59
- ( StatusCode :: BAD_GATEWAY , "BAD GATEWAY" ) . into_response ( )
60
- }
61
- ExternalErrorKind :: Other => {
62
- warn ! ( "ExternalErrorKind::Other: Responding with 500 Internal Server Error. Error: {:?}" , self ) ;
63
- ( StatusCode :: INTERNAL_SERVER_ERROR , "INTERNAL SERVER ERROR" ) . into_response ( )
64
- }
65
- }
40
+ match self {
41
+ Error :: Domain ( ref domain_error) => self . handle_domain_error ( domain_error) ,
42
+ Error :: Web ( ref web_error_kind) => self . handle_web_error ( web_error_kind) ,
43
+ }
44
+ }
45
+ }
46
+
47
+ impl Error {
48
+ fn handle_domain_error ( & self , domain_error : & DomainError ) -> Response {
49
+ match domain_error. error_kind {
50
+ DomainErrorKind :: Internal ( ref internal_error_kind) => {
51
+ self . handle_internal_error ( internal_error_kind)
52
+ }
53
+ DomainErrorKind :: External ( ref external_error_kind) => {
54
+ self . handle_external_error ( external_error_kind)
55
+ }
56
+ }
57
+ }
58
+
59
+ fn handle_internal_error ( & self , internal_error_kind : & InternalErrorKind ) -> Response {
60
+ match internal_error_kind {
61
+ InternalErrorKind :: Entity ( ref entity_error_kind) => {
62
+ self . handle_entity_error ( entity_error_kind)
63
+ }
64
+ InternalErrorKind :: Other => {
65
+ warn ! (
66
+ "InternalErrorKind::Other: Responding with 500 Internal Server Error. Error: {:?}" ,
67
+ self
68
+ ) ;
69
+ ( StatusCode :: INTERNAL_SERVER_ERROR , "INTERNAL SERVER ERROR" ) . into_response ( )
70
+ }
71
+ }
72
+ }
73
+
74
+ fn handle_entity_error ( & self , entity_error_kind : & EntityErrorKind ) -> Response {
75
+ match entity_error_kind {
76
+ EntityErrorKind :: NotFound => {
77
+ warn ! (
78
+ "EntityErrorKind::NotFound: Responding with 404 Not Found. Error: {:?}" ,
79
+ self
80
+ ) ;
81
+ ( StatusCode :: NOT_FOUND , "NOT FOUND" ) . into_response ( )
82
+ }
83
+ EntityErrorKind :: Invalid => {
84
+ warn ! (
85
+ "EntityErrorKind::Invalid: Responding with 422 Unprocessable Entity. Error: {:?}" ,
86
+ self
87
+ ) ;
88
+ ( StatusCode :: UNPROCESSABLE_ENTITY , "UNPROCESSABLE ENTITY" ) . into_response ( )
89
+ }
90
+ EntityErrorKind :: Other => {
91
+ warn ! (
92
+ "EntityErrorKind::Other: Responding with 500 Internal Server Error. Error: {:?}" ,
93
+ self
94
+ ) ;
95
+ ( StatusCode :: INTERNAL_SERVER_ERROR , "INTERNAL SERVER ERROR" ) . into_response ( )
96
+ }
97
+ }
98
+ }
99
+
100
+ fn handle_external_error ( & self , external_error_kind : & ExternalErrorKind ) -> Response {
101
+ match external_error_kind {
102
+ ExternalErrorKind :: Network => {
103
+ warn ! (
104
+ "ExternalErrorKind::Network: Responding with 502 Bad Gateway. Error: {:?}" ,
105
+ self
106
+ ) ;
107
+ ( StatusCode :: BAD_GATEWAY , "BAD GATEWAY" ) . into_response ( )
108
+ }
109
+ ExternalErrorKind :: Other => {
110
+ warn ! (
111
+ "ExternalErrorKind::Other: Responding with 500 Internal Server Error. Error: {:?}" ,
112
+ self
113
+ ) ;
114
+ ( StatusCode :: INTERNAL_SERVER_ERROR , "INTERNAL SERVER ERROR" ) . into_response ( )
115
+ }
116
+ }
117
+ }
118
+
119
+ fn handle_web_error ( & self , web_error_kind : & WebErrorKind ) -> Response {
120
+ match web_error_kind {
121
+ WebErrorKind :: Input => {
122
+ warn ! (
123
+ "WebErrorKind::Input: Responding with 400 Bad Request. Error: {:?}" ,
124
+ self
125
+ ) ;
126
+ ( StatusCode :: BAD_REQUEST , "BAD REQUEST" ) . into_response ( )
127
+ }
128
+ WebErrorKind :: Other => {
129
+ warn ! (
130
+ "WebErrorKind::Other: Responding with 500 Internal Server Error. Error: {:?}" ,
131
+ self
132
+ ) ;
133
+ ( StatusCode :: INTERNAL_SERVER_ERROR , "INTERNAL SERVER ERROR" ) . into_response ( )
66
134
}
67
135
}
68
136
}
73
141
E : Into < DomainError > ,
74
142
{
75
143
fn from ( err : E ) -> Self {
76
- Self ( err. into ( ) )
144
+ Error :: Domain ( err. into ( ) )
77
145
}
78
146
}
0 commit comments