Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/basic-error-anyhow/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
10 changes: 10 additions & 0 deletions examples/basic-error-anyhow/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
13 changes: 13 additions & 0 deletions examples/basic-error-anyhow/README.md
Original file line number Diff line number Diff line change
@@ -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`
21 changes: 21 additions & 0 deletions examples/basic-error-anyhow/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<Request>) -> anyhow::Result<()> {
bail!("This is an error message");
}

#[tokio::main]
async fn main() -> Result<(), Error> {
lambda_runtime::run(service_fn(|event: LambdaEvent<Request>| async move {
function_handler(event)
.await
.map_err(Into::<Box<dyn std::error::Error>>::into)
}))
.await
}
3 changes: 3 additions & 0 deletions lambda-runtime/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
Expand Down