Lambda function errors in Rust
Note
The Rust runtime client
When your code raises an error, Lambda generates a JSON representation of the error. This error
document appears in the invocation log and, for synchronous invocations, in the output. The Rust runtime client
Creating a function that returns errors
The following code sample shows a Lambda function that returns an error. The Rust Runtime handles this error directly.
use lambda_runtime::{service_fn, Error, LambdaEvent}; use serde_json::{json, Value}; async fn handler(_event: LambdaEvent<Value>) -> Result<Value, String> { Err("something went wrong!".into()) } #[tokio::main] async fn main() -> Result<(), Error> { lambda_runtime::run(service_fn(handler)).await }
This code returns the following error payload:
{ "errorType": "&alloc::string::String", "errorMessage": "something went wrong!" }
For a more advanced error handling example, see the sample application