File tree 4 files changed +45
-0
lines changed
examples/basic-error-anyhow
4 files changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ /target
Original file line number Diff line number Diff line change
1
+ [package ]
2
+ name = " basic-error-anyhow"
3
+ version = " 0.1.0"
4
+ edition = " 2021"
5
+
6
+ [dependencies ]
7
+ anyhow = " 1"
8
+ lambda_runtime = { path = " ../../lambda-runtime" }
9
+ serde = " 1"
10
+ tokio = { version = " 1" , features = [" macros" ] }
Original file line number Diff line number Diff line change
1
+ # AWS Lambda Function Error Handling With ` anyhow ` Crate Example
2
+
3
+ This example shows how to use external error types like ` anyhow::Error ` .
4
+
5
+ ## Build & Deploy
6
+
7
+ 1 . Install [ cargo-lambda] ( https://github.com/cargo-lambda/cargo-lambda#installation )
8
+ 2 . Build the function with ` cargo lambda build --release `
9
+ 3 . Deploy the function to AWS Lambda with ` cargo lambda deploy --iam-role YOUR_ROLE `
10
+
11
+ ## Build for ARM 64
12
+
13
+ Build the function with ` cargo lambda build --release --arm64 `
Original file line number Diff line number Diff line change
1
+ use anyhow:: bail;
2
+ use lambda_runtime:: { service_fn, Error , LambdaEvent } ;
3
+ use serde:: Deserialize ;
4
+
5
+ #[ derive( Deserialize ) ]
6
+ struct Request { }
7
+
8
+ /// Return anyhow::Result in the main body for the Lambda function.
9
+ async fn function_handler ( _event : LambdaEvent < Request > ) -> anyhow:: Result < ( ) > {
10
+ bail ! ( "This is an error message" ) ;
11
+ }
12
+
13
+ #[ tokio:: main]
14
+ async fn main ( ) -> Result < ( ) , Error > {
15
+ lambda_runtime:: run ( service_fn ( |event : LambdaEvent < Request > | async move {
16
+ function_handler ( event)
17
+ . await
18
+ . map_err ( Into :: < Box < dyn std:: error:: Error > > :: into)
19
+ } ) )
20
+ . await
21
+ }
You can’t perform that action at this time.
0 commit comments