-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relax blanket implementation of Diagnostic (#897)
Instead of implementing Diagnostic for everything that implements Display, implement the trait only for a few well known types. This gives people more flexibility to implement Diagnostic. Signed-off-by: David Calavera <[email protected]>
- Loading branch information
Showing
17 changed files
with
251 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
examples/advanced-sqs-multiple-functions-shared-data/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
[workspace] | ||
resolver = "2" | ||
|
||
members = [ | ||
"producer", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "basic-error-diagnostic" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# Starting in Rust 1.62 you can use `cargo add` to add dependencies | ||
# to your project. | ||
# | ||
# If you're using an older Rust version, | ||
# download cargo-edit(https://github.com/killercup/cargo-edit#installation) | ||
# to install the `add` subcommand. | ||
# | ||
# Running `cargo add DEPENDENCY_NAME` will | ||
# add the latest version of a dependency to the list, | ||
# and it will keep the alphabetic ordering for you. | ||
|
||
[dependencies] | ||
|
||
lambda_runtime = { path = "../../lambda-runtime" } | ||
serde = "1" | ||
thiserror = "1.0.61" | ||
tokio = { version = "1", features = ["macros"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# AWS Lambda Function Error handling example | ||
|
||
This example shows how to implement the `Diagnostic` trait to return a specific `error_type` in the Lambda error response. If you don't use the `error_type` field, you don't need to implement `Diagnostic`, the type will be generated based on the error type name. | ||
|
||
## Build & Deploy | ||
|
||
1. Install [cargo-lambda](https://github.com/cargo-lambda/cargo-lambda#installation) | ||
2. Build the function with `cargo lambda build --release` | ||
3. Deploy the function to AWS Lambda with `cargo lambda deploy --iam-role YOUR_ROLE` | ||
|
||
## Build for ARM 64 | ||
|
||
Build the function with `cargo lambda build --release --arm64` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use lambda_runtime::{service_fn, Diagnostic, Error, LambdaEvent}; | ||
use serde::Deserialize; | ||
use thiserror; | ||
|
||
#[derive(Deserialize)] | ||
struct Request {} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum ExecutionError { | ||
#[error("transient database error: {0}")] | ||
DatabaseError(String), | ||
#[error("unexpected error: {0}")] | ||
Unexpected(String), | ||
} | ||
|
||
impl<'a> From<ExecutionError> for Diagnostic<'a> { | ||
fn from(value: ExecutionError) -> Diagnostic<'a> { | ||
let (error_type, error_message) = match value { | ||
ExecutionError::DatabaseError(err) => ("Retryable", err.to_string()), | ||
ExecutionError::Unexpected(err) => ("NonRetryable", err.to_string()), | ||
}; | ||
Diagnostic { | ||
error_type: error_type.into(), | ||
error_message: error_message.into(), | ||
} | ||
} | ||
} | ||
|
||
/// This is the main body for the Lambda function | ||
async fn function_handler(_event: LambdaEvent<Request>) -> Result<(), ExecutionError> { | ||
Err(ExecutionError::Unexpected("ooops".to_string())) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
lambda_runtime::run(service_fn(function_handler)).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::{any::type_name, borrow::Cow}; | ||
|
||
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]. | ||
/// | ||
/// [`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. | ||
/// You can define your own error container that implements `Into<Diagnostic>` | ||
/// if you need to handle errors based on error types. | ||
/// | ||
/// Example: | ||
/// ``` | ||
/// use lambda_runtime::{Diagnostic, Error, LambdaEvent}; | ||
/// use std::borrow::Cow; | ||
/// | ||
/// #[derive(Debug)] | ||
/// struct ErrorResponse(Error); | ||
/// | ||
/// impl<'a> Into<Diagnostic<'a>> for ErrorResponse { | ||
/// fn into(self) -> Diagnostic<'a> { | ||
/// Diagnostic { | ||
/// error_type: "MyError".into(), | ||
/// error_message: self.0.to_string().into(), | ||
/// } | ||
/// } | ||
/// } | ||
/// | ||
/// async fn function_handler(_event: LambdaEvent<()>) -> Result<(), ErrorResponse> { | ||
/// // ... do something | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Diagnostic<'a> { | ||
/// Error type. | ||
/// | ||
/// `error_type` is derived from the type name of the original error with | ||
/// [`std::any::type_name`] as a fallback. | ||
/// Please implement your own `Into<Diagnostic>` if you need more reliable | ||
/// error types. | ||
pub error_type: Cow<'a, str>, | ||
/// Error message. | ||
/// | ||
/// `error_message` is the output from the [`Display`][std::fmt::Display] | ||
/// implementation of the original error as a fallback. | ||
pub error_message: Cow<'a, str>, | ||
} | ||
|
||
impl<'a> From<DeserializeError> for Diagnostic<'a> { | ||
fn from(value: DeserializeError) -> Self { | ||
Diagnostic { | ||
error_type: type_name::<DeserializeError>().into(), | ||
error_message: value.to_string().into(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> From<Error> for Diagnostic<'a> { | ||
fn from(value: Error) -> Self { | ||
Diagnostic { | ||
error_type: type_name::<Error>().into(), | ||
error_message: value.to_string().into(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a, T> From<Box<T>> for Diagnostic<'a> | ||
where | ||
T: std::error::Error, | ||
{ | ||
fn from(value: Box<T>) -> Self { | ||
Diagnostic { | ||
error_type: type_name::<T>().into(), | ||
error_message: value.to_string().into(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> From<Box<dyn std::error::Error>> for Diagnostic<'a> { | ||
fn from(value: Box<dyn std::error::Error>) -> Self { | ||
Diagnostic { | ||
error_type: type_name::<Box<dyn std::error::Error>>().into(), | ||
error_message: value.to_string().into(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> From<std::convert::Infallible> for Diagnostic<'a> { | ||
fn from(value: std::convert::Infallible) -> Self { | ||
Diagnostic { | ||
error_type: type_name::<std::convert::Infallible>().into(), | ||
error_message: value.to_string().into(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> From<String> for Diagnostic<'a> { | ||
fn from(value: String) -> Self { | ||
Diagnostic { | ||
error_type: type_name::<String>().into(), | ||
error_message: value.into(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> From<&'static str> for Diagnostic<'a> { | ||
fn from(value: &'static str) -> Self { | ||
Diagnostic { | ||
error_type: type_name::<&'static str>().into(), | ||
error_message: value.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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.