Repository navigation
Expand file tree
/
Copy pathdiagnostic.rs
More file actions
207 lines (188 loc) · 6.38 KB
/
Copy pathdiagnostic.rs
File metadata and controls
207 lines (188 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use serde::{Deserialize, Serialize};
use std::any::type_name;
use crate::{deserializer::DeserializeError, Error};
/// Diagnostic information about an error.
///
/// `Diagnostic` is automatically derived for some common types,
/// like boxed types that implement [`Error`][std::error::Error].
/// If you use an error type which comes from a external crate like anyhow,
/// you need convert it to common types like `Box<dyn std::error::Error>`.
/// See the examples for more details.
///
/// [`error_type`][`Diagnostic::error_type`] is derived from the type name of
/// the original error with [`std::any::type_name`] as a fallback, which may
/// not be reliable for conditional error handling.
///
/// To get more descriptive [`error_type`][`Diagnostic::error_type`] fields, you can implement `From` for your error type.
/// That gives you full control on what the `error_type` is.
///
/// Example:
/// ```
/// use lambda_runtime::{Diagnostic, Error, LambdaEvent};
///
/// #[derive(Debug)]
/// struct ErrorResponse(&'static str);
///
/// impl From<ErrorResponse> for Diagnostic {
/// fn from(error: ErrorResponse) -> Diagnostic {
/// Diagnostic {
/// error_type: "MyError".into(),
/// error_message: error.0.to_string(),
/// }
/// }
/// }
///
/// async fn function_handler(_event: LambdaEvent<()>) -> Result<(), ErrorResponse> {
/// Err(ErrorResponse("this is an error response"))
/// }
/// ```
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Diagnostic {
/// `error_type` is the type of exception or error returned by the function.
/// Use this field to categorize the different kinds of errors that your function
/// might experience.
///
/// In standard implementations, `error_type` is derived from the type name of the original error with
/// [`std::any::type_name`], however this is not descriptive enough for an error type.
/// Implement your own `Into<Diagnostic>` to return a more descriptive error type.
pub error_type: String,
/// `error_message` is a string expression of the error.
/// In standard implementations, it's the output from the [`Display`][std::fmt::Display]
/// implementation of the original error.
pub error_message: String,
}
impl From<DeserializeError> for Diagnostic {
fn from(value: DeserializeError) -> Self {
Diagnostic {
error_type: type_name_of_val(&value),
error_message: value.to_string(),
}
}
}
impl From<Error> for Diagnostic {
fn from(value: Error) -> Self {
Diagnostic {
error_type: type_name_of_val(&value),
error_message: value.to_string(),
}
}
}
impl From<Box<dyn std::error::Error>> for Diagnostic {
fn from(value: Box<dyn std::error::Error>) -> Self {
Diagnostic {
error_type: type_name_of_val(&value),
error_message: value.to_string(),
}
}
}
impl From<std::convert::Infallible> for Diagnostic {
fn from(value: std::convert::Infallible) -> Self {
Diagnostic {
error_type: type_name_of_val(&value),
error_message: value.to_string(),
}
}
}
impl From<String> for Diagnostic {
fn from(value: String) -> Self {
Diagnostic {
error_type: type_name_of_val(&value),
error_message: value.to_string(),
}
}
}
impl From<&'static str> for Diagnostic {
fn from(value: &'static str) -> Self {
Diagnostic {
error_type: type_name_of_val(&value),
error_message: value.to_string(),
}
}
}
impl From<std::io::Error> for Diagnostic {
fn from(value: std::io::Error) -> Self {
Diagnostic {
error_type: type_name_of_val(&value),
error_message: value.to_string(),
}
}
}
#[cfg(feature = "anyhow")]
#[cfg_attr(docsrs, doc(cfg(feature = "anyhow")))]
impl From<anyhow::Error> for Diagnostic {
fn from(value: anyhow::Error) -> Diagnostic {
Diagnostic {
error_type: type_name_of_val(&value),
error_message: value.to_string(),
}
}
}
#[cfg(feature = "eyre")]
#[cfg_attr(docsrs, doc(cfg(feature = "eyre")))]
impl From<eyre::Report> for Diagnostic {
fn from(value: eyre::Report) -> Diagnostic {
Diagnostic {
error_type: type_name_of_val(&value),
error_message: value.to_string(),
}
}
}
#[cfg(feature = "miette")]
#[cfg_attr(docsrs, doc(cfg(feature = "miette")))]
impl From<miette::Report> for Diagnostic {
fn from(value: miette::Report) -> Diagnostic {
Diagnostic {
error_type: type_name_of_val(&value),
error_message: value.to_string(),
}
}
}
pub(crate) fn type_name_of_val<T>(_: T) -> String {
type_name::<T>().into()
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn round_trip_lambda_error() {
use serde_json::{json, Value};
let expected = json!({
"errorType": "InvalidEventDataError",
"errorMessage": "Error parsing event data.",
});
let actual = Diagnostic {
error_type: "InvalidEventDataError".into(),
error_message: "Error parsing event data.".into(),
};
let actual: Value = serde_json::to_value(actual).expect("failed to serialize diagnostic");
assert_eq!(expected, actual);
}
#[cfg(feature = "anyhow")]
#[test]
fn test_anyhow_integration() {
use anyhow::Error as AnyhowError;
let error: AnyhowError = anyhow::anyhow!("anyhow error");
let diagnostic: Diagnostic = error.into();
assert_eq!(diagnostic.error_type, "&anyhow::Error");
assert_eq!(diagnostic.error_message, "anyhow error");
}
#[cfg(feature = "eyre")]
#[test]
fn test_eyre_integration() {
use eyre::Report;
let error: Report = eyre::eyre!("eyre error");
let diagnostic: Diagnostic = error.into();
assert_eq!(diagnostic.error_type, "&eyre::Report");
assert_eq!(diagnostic.error_message, "eyre error");
}
#[cfg(feature = "miette")]
#[test]
fn test_miette_integration() {
use miette::Report;
let error: Report = miette::miette!("miette error");
let diagnostic: Diagnostic = error.into();
assert_eq!(diagnostic.error_type, "&miette::eyreish::Report");
assert_eq!(diagnostic.error_message, "miette error");
}
}