|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | +use std::borrow::Cow; |
| 3 | + |
| 4 | +use crate::{deserializer::DeserializeError, Error}; |
| 5 | + |
| 6 | +/// Diagnostic information about an error. |
| 7 | +/// |
| 8 | +/// `Diagnostic` is automatically derived for some common types, |
| 9 | +/// like boxed types that implement [`Error`][std::error::Error]. |
| 10 | +/// |
| 11 | +/// [`error_type`][`Diagnostic::error_type`] is derived from the type name of |
| 12 | +/// the original error with [`std::any::type_name`] as a fallback, which may |
| 13 | +/// not be reliable for conditional error handling. |
| 14 | +/// You can define your own error container that implements `Into<Diagnostic>` |
| 15 | +/// if you need to handle errors based on error types. |
| 16 | +/// |
| 17 | +/// Example: |
| 18 | +/// ``` |
| 19 | +/// use lambda_runtime::{Diagnostic, Error, LambdaEvent}; |
| 20 | +/// use std::borrow::Cow; |
| 21 | +/// |
| 22 | +/// #[derive(Debug)] |
| 23 | +/// struct ErrorResponse(Error); |
| 24 | +/// |
| 25 | +/// impl<'a> Into<Diagnostic<'a>> for ErrorResponse { |
| 26 | +/// fn into(self) -> Diagnostic<'a> { |
| 27 | +/// Diagnostic { |
| 28 | +/// error_type: "MyError".into(), |
| 29 | +/// error_message: self.0.to_string().into(), |
| 30 | +/// } |
| 31 | +/// } |
| 32 | +/// } |
| 33 | +/// |
| 34 | +/// async fn function_handler(_event: LambdaEvent<()>) -> Result<(), ErrorResponse> { |
| 35 | +/// // ... do something |
| 36 | +/// Ok(()) |
| 37 | +/// } |
| 38 | +/// ``` |
| 39 | +#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)] |
| 40 | +#[serde(rename_all = "camelCase")] |
| 41 | +pub struct Diagnostic<'a> { |
| 42 | + /// Error type. |
| 43 | + /// |
| 44 | + /// `error_type` is derived from the type name of the original error with |
| 45 | + /// [`std::any::type_name`] as a fallback. |
| 46 | + /// Please implement your own `Into<Diagnostic>` if you need more reliable |
| 47 | + /// error types. |
| 48 | + pub error_type: Cow<'a, str>, |
| 49 | + /// Error message. |
| 50 | + /// |
| 51 | + /// `error_message` is the output from the [`Display`][std::fmt::Display] |
| 52 | + /// implementation of the original error as a fallback. |
| 53 | + pub error_message: Cow<'a, str>, |
| 54 | +} |
| 55 | + |
| 56 | +impl<'a> From<DeserializeError> for Diagnostic<'a> { |
| 57 | + fn from(value: DeserializeError) -> Self { |
| 58 | + Diagnostic { |
| 59 | + error_type: std::any::type_name::<DeserializeError>().into(), |
| 60 | + error_message: value.to_string().into(), |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<'a> From<Error> for Diagnostic<'a> { |
| 66 | + fn from(value: Error) -> Self { |
| 67 | + Diagnostic { |
| 68 | + error_type: std::any::type_name::<Error>().into(), |
| 69 | + error_message: value.to_string().into(), |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl<'a, T> From<Box<T>> for Diagnostic<'a> |
| 75 | +where |
| 76 | + T: std::error::Error, |
| 77 | +{ |
| 78 | + fn from(value: Box<T>) -> Self { |
| 79 | + Diagnostic { |
| 80 | + error_type: std::any::type_name::<T>().into(), |
| 81 | + error_message: value.to_string().into(), |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl<'a> From<Box<dyn std::error::Error>> for Diagnostic<'a> { |
| 87 | + fn from(value: Box<dyn std::error::Error>) -> Self { |
| 88 | + Diagnostic { |
| 89 | + error_type: std::any::type_name::<Box<dyn std::error::Error>>().into(), |
| 90 | + error_message: value.to_string().into(), |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl<'a> From<std::convert::Infallible> for Diagnostic<'a> { |
| 96 | + fn from(value: std::convert::Infallible) -> Self { |
| 97 | + Diagnostic { |
| 98 | + error_type: std::any::type_name::<std::convert::Infallible>().into(), |
| 99 | + error_message: value.to_string().into(), |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +#[cfg(test)] |
| 105 | +mod test { |
| 106 | + use super::*; |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn round_trip_lambda_error() { |
| 110 | + use serde_json::{json, Value}; |
| 111 | + let expected = json!({ |
| 112 | + "errorType": "InvalidEventDataError", |
| 113 | + "errorMessage": "Error parsing event data.", |
| 114 | + }); |
| 115 | + |
| 116 | + let actual = Diagnostic { |
| 117 | + error_type: "InvalidEventDataError".into(), |
| 118 | + error_message: "Error parsing event data.".into(), |
| 119 | + }; |
| 120 | + let actual: Value = serde_json::to_value(actual).expect("failed to serialize diagnostic"); |
| 121 | + assert_eq!(expected, actual); |
| 122 | + } |
| 123 | +} |
0 commit comments