From 10d991be9dc2fbecdb910f44b8fd2efb9373100f Mon Sep 17 00:00:00 2001 From: Taiki Ono Date: Mon, 1 Jul 2024 19:43:38 +0900 Subject: [PATCH 1/2] Add "error handling with anyhow" example Signed-off-by: Taiki Ono --- examples/basic-error-anyhow/.gitignore | 1 + examples/basic-error-anyhow/Cargo.toml | 10 ++++++++++ examples/basic-error-anyhow/README.md | 13 +++++++++++++ examples/basic-error-anyhow/src/main.rs | 21 +++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 examples/basic-error-anyhow/.gitignore create mode 100644 examples/basic-error-anyhow/Cargo.toml create mode 100644 examples/basic-error-anyhow/README.md create mode 100644 examples/basic-error-anyhow/src/main.rs diff --git a/examples/basic-error-anyhow/.gitignore b/examples/basic-error-anyhow/.gitignore new file mode 100644 index 00000000..ea8c4bf7 --- /dev/null +++ b/examples/basic-error-anyhow/.gitignore @@ -0,0 +1 @@ +/target diff --git a/examples/basic-error-anyhow/Cargo.toml b/examples/basic-error-anyhow/Cargo.toml new file mode 100644 index 00000000..a0ff62db --- /dev/null +++ b/examples/basic-error-anyhow/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "basic-error-anyhow" +version = "0.1.0" +edition = "2021" + +[dependencies] +anyhow = "1" +lambda_runtime = { path = "../../lambda-runtime" } +serde = "1" +tokio = { version = "1", features = ["macros"] } diff --git a/examples/basic-error-anyhow/README.md b/examples/basic-error-anyhow/README.md new file mode 100644 index 00000000..b659c283 --- /dev/null +++ b/examples/basic-error-anyhow/README.md @@ -0,0 +1,13 @@ +# AWS Lambda Function Error Handling With `anyhow` Crate Example + +This example shows how to use external error types like `anyhow::Error`. + +## 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` diff --git a/examples/basic-error-anyhow/src/main.rs b/examples/basic-error-anyhow/src/main.rs new file mode 100644 index 00000000..cbca84fd --- /dev/null +++ b/examples/basic-error-anyhow/src/main.rs @@ -0,0 +1,21 @@ +use anyhow::bail; +use lambda_runtime::{service_fn, Error, LambdaEvent}; +use serde::Deserialize; + +#[derive(Deserialize)] +struct Request {} + +/// Return anyhow::Result in the main body for the Lambda function. +async fn function_handler(_event: LambdaEvent) -> anyhow::Result<()> { + bail!("This is an error message"); +} + +#[tokio::main] +async fn main() -> Result<(), Error> { + lambda_runtime::run(service_fn(|event: LambdaEvent| async move { + function_handler(event) + .await + .map_err(Into::>::into) + })) + .await +} From 0f71e927835ca5b3eed2b84345bb8b66fddb0b2f Mon Sep 17 00:00:00 2001 From: Taiki Ono Date: Mon, 1 Jul 2024 20:00:22 +0900 Subject: [PATCH 2/2] More doc for users who use external error types Signed-off-by: Taiki Ono --- lambda-runtime/src/diagnostic.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lambda-runtime/src/diagnostic.rs b/lambda-runtime/src/diagnostic.rs index bc9ba623..9a7230a7 100644 --- a/lambda-runtime/src/diagnostic.rs +++ b/lambda-runtime/src/diagnostic.rs @@ -7,6 +7,9 @@ use crate::{deserializer::DeserializeError, 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`. +/// 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