You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Change how we handle error types to be more ergonomic:
- Replace Cow with String: Diagnostics are serialized into JSON as soon as the function returns, which means that their value is copied right away. The performance improvement of using Cow is minimal in this case, but it has some ergonomic implications because we have to handle their lifetimes. By removing the explicit lifetimes, people can return Diagnostic values with static lifetimes which was not possible before.
- Add `IntoDiagnostic` trait. This is a helper trait to facilitate transforming value types into Diagnostic. It gives external crates a better mechanism to transform values into `Diagnostic`.
- Add features to implement `IntoDiagnostic` for anyhow, eyre, and miette error types. This helps people that use those creates to transform their errors into `Diagnostic` without double boxing their errors.
Copy file name to clipboardExpand all lines: README.md
+56-1Lines changed: 56 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,7 @@ pip3 install cargo-lambda
40
40
41
41
See other installation options in [the Cargo Lambda documentation](https://www.cargo-lambda.info/guide/installation.html).
42
42
43
-
###Your first function
43
+
## Your first function
44
44
45
45
To create your first function, run Cargo Lambda with the [subcommand `new`](https://www.cargo-lambda.info/commands/new.html). This command will generate a Rust package with the initial source code for your function:
when a function invocation fails, AWS Lambda expects you to return an object that can be serialized into JSON structure with the error information. This structure is represented in the following example:
77
+
78
+
```json
79
+
{
80
+
"error_type": "the type of error raised",
81
+
"error_message": "a string description of the error"
82
+
}
83
+
```
84
+
85
+
The Rust Runtime for Lambda uses a struct called `Diagnostic` to represent function errors internally. The runtime implements the converstion of several general errors types, like `std::error::Error`, into `Diagnostic`. For these general implementations, the `error_type` is the name of the value type returned by your function. For example, if your function returns `lambda_runtime::Error`, the `error_type` will be something like `alloc::boxed::Box<dyn core::error::Error + core::marker::Send + core::marker::Sync>`, which is not very descriptive.
86
+
87
+
### Implement your own Diagnostic
88
+
89
+
To get more descriptive `error_type` fields, you can implement `Into<Diagnostic>` for your error type. That gives you full control on what the `error_type` is:
We recommend you to use the [thiserror crate](https://crates.io/crates/thiserror) to declare your errors. You can see an example on how to integrate `thiserror` with the Runtime's diagnostics in our [example repository](https://github.com/awslabs/aws-lambda-rust-runtime/tree/main/examples/basic-error-thiserror)
112
+
113
+
### Anyhow, Eyre, and Miette
114
+
115
+
Popular error crates like Anyhow, Eyre, and Miette provide their own error types that encapsulate other errors. There is no direct transformation of those errors into `Diagnostic`, but we provide feature flags for each one of those crates to help you integrate them with your Lambda functions.
116
+
117
+
If you enable the features `anyhow`, `eyre`, or `miette` in the `lambda_runtime` dependency of your package. The error types provided by those crates can have blanket transformations into `Diagnostic` when the `lambda_runtime::IntoDiagnostic` trait is in scope. This trait exposes an `into_diagnostic` method that transforms those error types into a `Diagnostic`. This is an example that transforms an `anyhow::Error` into a `Diagnostic`:
Err(anyhow::anyhow!("this is an error").into_diagnostic())
124
+
}
125
+
```
126
+
127
+
You can see more examples on how to use these error crates in our [example repository](https://github.com/awslabs/aws-lambda-rust-runtime/tree/main/examples/basic-error-error-crates-integration).
128
+
74
129
## Building and deploying your Lambda functions
75
130
76
131
If you already have Cargo Lambda installed in your machine, run the next command to build your function:
Copy file name to clipboardExpand all lines: examples/basic-error-error-crates-integration/README.md
+4-2Lines changed: 4 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
-
# AWS Lambda Function Error Handling With `anyhow` Crate Example
1
+
# AWS Lambda Function Error Handling with several popular error crates.
2
2
3
-
This example shows how to use external error types like `anyhow::Error`.
3
+
This example shows how to use external error types like `anyhow::Error`, `eyre::Report`, and `miette::Report`.
4
+
5
+
To use the integrations with these crates, you need to enable to respective feature flag in the runtime which provides the implemetation of `into_diagnostic` for specific error types provided by these crates.
0 commit comments