@@ -55,124 +55,21 @@ pub struct Diagnostic {
55
55
pub error_message : String ,
56
56
}
57
57
58
- /// Trait that adds an `into_diagnostic` method to several types,
59
- /// including standard errors, and error types from several well known error crates.
60
- ///
61
- /// This trait is automatically implemented for some common types,
62
- /// like boxed types that implement [`Error`][std::error::Error].
63
- /// If you use an error type which comes from a external crate like anyhow, eyre, or miette,
64
- /// you can enable the features `anyhow`, `eyre` or `miette` in the `lambda_runtime` crate to automatically
65
- /// implement `IntoDiagnostic`.
66
- ///
67
- /// Example:
68
- /// ```
69
- /// use lambda_runtime::{Diagnostic, IntoDiagnostic, LambdaEvent};
70
- ///
71
- /// #[derive(Debug)]
72
- /// struct ErrorResponse(&'static str);
73
- ///
74
- /// impl IntoDiagnostic for ErrorResponse {
75
- /// fn into_diagnostic(self) -> Diagnostic {
76
- /// Diagnostic {
77
- /// error_type: "MyError".into(),
78
- /// error_message: self.0.into(),
79
- /// }
80
- /// }
81
- /// }
82
- ///
83
- /// async fn function_handler(_event: LambdaEvent<()>) -> Result<(), Diagnostic> {
84
- /// Err(ErrorResponse("this is an error response").into_diagnostic())
85
- /// }
86
- /// ```
87
- pub trait IntoDiagnostic {
88
- /// Converts external error types into [`Diagnostic`]
89
- fn into_diagnostic ( self ) -> Diagnostic ;
90
- }
91
-
92
- impl IntoDiagnostic for DeserializeError {
93
- fn into_diagnostic ( self ) -> Diagnostic {
94
- Diagnostic {
95
- error_type : type_name_of_val ( & self ) ,
96
- error_message : self . to_string ( ) ,
97
- }
98
- }
99
- }
100
-
101
- impl IntoDiagnostic for Error {
102
- fn into_diagnostic ( self ) -> Diagnostic {
103
- Diagnostic {
104
- error_type : type_name_of_val ( & self ) ,
105
- error_message : self . to_string ( ) ,
106
- }
107
- }
108
- }
109
-
110
- impl IntoDiagnostic for Box < dyn std:: error:: Error > {
111
- fn into_diagnostic ( self ) -> Diagnostic {
112
- Diagnostic {
113
- error_type : type_name_of_val ( & self ) ,
114
- error_message : self . to_string ( ) ,
115
- }
116
- }
117
- }
118
-
119
- impl IntoDiagnostic for std:: convert:: Infallible {
120
- fn into_diagnostic ( self ) -> Diagnostic {
121
- Diagnostic {
122
- error_type : type_name_of_val ( & self ) ,
123
- error_message : self . to_string ( ) ,
124
- }
125
- }
126
- }
127
-
128
- impl IntoDiagnostic for String {
129
- fn into_diagnostic ( self ) -> Diagnostic {
130
- Diagnostic {
131
- error_type : type_name_of_val ( & self ) ,
132
- error_message : self ,
133
- }
134
- }
135
- }
136
-
137
- impl IntoDiagnostic for & ' static str {
138
- fn into_diagnostic ( self ) -> Diagnostic {
139
- Diagnostic {
140
- error_type : type_name_of_val ( & self ) ,
141
- error_message : self . into ( ) ,
142
- }
143
- }
144
- }
145
-
146
- impl IntoDiagnostic for std:: io:: Error {
147
- fn into_diagnostic ( self ) -> Diagnostic {
148
- Diagnostic {
149
- error_type : type_name_of_val ( & self ) ,
150
- error_message : self . to_string ( ) ,
151
- }
152
- }
153
- }
154
-
155
- impl < T > IntoDiagnostic for Box < T >
156
- where
157
- T : std:: error:: Error ,
158
- {
159
- fn into_diagnostic ( self ) -> Diagnostic {
160
- Diagnostic {
161
- error_type : type_name_of_val ( & self ) ,
162
- error_message : self . to_string ( ) ,
163
- }
164
- }
165
- }
166
-
167
58
impl From < DeserializeError > for Diagnostic {
168
59
fn from ( value : DeserializeError ) -> Self {
169
- value. into_diagnostic ( )
60
+ Diagnostic {
61
+ error_type : type_name_of_val ( & value) ,
62
+ error_message : value. to_string ( ) ,
63
+ }
170
64
}
171
65
}
172
66
173
67
impl From < Error > for Diagnostic {
174
68
fn from ( value : Error ) -> Self {
175
- value. into_diagnostic ( )
69
+ Diagnostic {
70
+ error_type : type_name_of_val ( & value) ,
71
+ error_message : value. to_string ( ) ,
72
+ }
176
73
}
177
74
}
178
75
@@ -181,66 +78,84 @@ where
181
78
T : std:: error:: Error ,
182
79
{
183
80
fn from ( value : Box < T > ) -> Self {
184
- value. into_diagnostic ( )
81
+ Diagnostic {
82
+ error_type : type_name_of_val ( & value) ,
83
+ error_message : value. to_string ( ) ,
84
+ }
185
85
}
186
86
}
187
87
188
88
impl From < Box < dyn std:: error:: Error > > for Diagnostic {
189
89
fn from ( value : Box < dyn std:: error:: Error > ) -> Self {
190
- value. into_diagnostic ( )
90
+ Diagnostic {
91
+ error_type : type_name_of_val ( & value) ,
92
+ error_message : value. to_string ( ) ,
93
+ }
191
94
}
192
95
}
193
96
194
97
impl From < std:: convert:: Infallible > for Diagnostic {
195
98
fn from ( value : std:: convert:: Infallible ) -> Self {
196
- value. into_diagnostic ( )
99
+ Diagnostic {
100
+ error_type : type_name_of_val ( & value) ,
101
+ error_message : value. to_string ( ) ,
102
+ }
197
103
}
198
104
}
199
105
200
106
impl From < String > for Diagnostic {
201
107
fn from ( value : String ) -> Self {
202
- value. into_diagnostic ( )
108
+ Diagnostic {
109
+ error_type : type_name_of_val ( & value) ,
110
+ error_message : value. to_string ( ) ,
111
+ }
203
112
}
204
113
}
205
114
206
115
impl From < & ' static str > for Diagnostic {
207
116
fn from ( value : & ' static str ) -> Self {
208
- value. into_diagnostic ( )
117
+ Diagnostic {
118
+ error_type : type_name_of_val ( & value) ,
119
+ error_message : value. to_string ( ) ,
120
+ }
209
121
}
210
122
}
211
123
212
124
impl From < std:: io:: Error > for Diagnostic {
213
125
fn from ( value : std:: io:: Error ) -> Self {
214
- value. into_diagnostic ( )
126
+ Diagnostic {
127
+ error_type : type_name_of_val ( & value) ,
128
+ error_message : value. to_string ( ) ,
129
+ }
215
130
}
216
131
}
217
132
218
133
#[ cfg( feature = "anyhow" ) ]
219
- impl IntoDiagnostic for anyhow:: Error {
220
- fn into_diagnostic ( self ) -> Diagnostic {
134
+ impl From < anyhow:: Error > for Diagnostic {
135
+ fn from ( value : anyhow :: Error ) -> Diagnostic {
221
136
Diagnostic {
222
- error_type : type_name_of_val ( & self ) ,
223
- error_message : self . to_string ( ) ,
137
+ error_type : type_name_of_val ( & value ) ,
138
+ error_message : value . to_string ( ) ,
224
139
}
225
140
}
226
141
}
227
142
228
143
#[ cfg( feature = "eyre" ) ]
229
- impl IntoDiagnostic for eyre:: Report {
230
- fn into_diagnostic ( self ) -> Diagnostic {
144
+ impl From < eyre:: Report > for Diagnostic {
145
+ fn from ( value : eyre :: Report ) -> Diagnostic {
231
146
Diagnostic {
232
- error_type : type_name_of_val ( & self ) ,
233
- error_message : self . to_string ( ) ,
147
+ error_type : type_name_of_val ( & value ) ,
148
+ error_message : value . to_string ( ) ,
234
149
}
235
150
}
236
151
}
237
152
238
153
#[ cfg( feature = "miette" ) ]
239
- impl IntoDiagnostic for miette:: Report {
240
- fn into_diagnostic ( self ) -> Diagnostic {
154
+ impl From < miette:: Report > for Diagnostic {
155
+ fn from ( value : miette :: Report ) -> Diagnostic {
241
156
Diagnostic {
242
- error_type : type_name_of_val ( & self ) ,
243
- error_message : self . to_string ( ) ,
157
+ error_type : type_name_of_val ( & value ) ,
158
+ error_message : value . to_string ( ) ,
244
159
}
245
160
}
246
161
}
@@ -274,7 +189,7 @@ mod test {
274
189
fn test_anyhow_integration ( ) {
275
190
use anyhow:: Error as AnyhowError ;
276
191
let error: AnyhowError = anyhow:: anyhow!( "anyhow error" ) ;
277
- let diagnostic: Diagnostic = error. into_diagnostic ( ) ;
192
+ let diagnostic: Diagnostic = error. into ( ) ;
278
193
assert_eq ! ( diagnostic. error_type, "&anyhow::Error" ) ;
279
194
assert_eq ! ( diagnostic. error_message, "anyhow error" ) ;
280
195
}
@@ -284,7 +199,7 @@ mod test {
284
199
fn test_eyre_integration ( ) {
285
200
use eyre:: Report ;
286
201
let error: Report = eyre:: eyre!( "eyre error" ) ;
287
- let diagnostic: Diagnostic = error. into_diagnostic ( ) ;
202
+ let diagnostic: Diagnostic = error. into ( ) ;
288
203
assert_eq ! ( diagnostic. error_type, "&eyre::Report" ) ;
289
204
assert_eq ! ( diagnostic. error_message, "eyre error" ) ;
290
205
}
@@ -294,7 +209,7 @@ mod test {
294
209
fn test_miette_integration ( ) {
295
210
use miette:: Report ;
296
211
let error: Report = miette:: miette!( "miette error" ) ;
297
- let diagnostic: Diagnostic = error. into_diagnostic ( ) ;
212
+ let diagnostic: Diagnostic = error. into ( ) ;
298
213
assert_eq ! ( diagnostic. error_type, "&miette::eyreish::Report" ) ;
299
214
assert_eq ! ( diagnostic. error_message, "miette error" ) ;
300
215
}
0 commit comments