Rust 中的 Lambda 函数错误 - Amazon Lambda
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

Rust 中的 Lambda 函数错误

注意

Rust 运行时系统客户端是实验性程序包。它随时可能更改,并且仅用于评估目的。

您的代码引发错误时,Lambda 将会生成错误的 JSON 表示形式。此错误文档会出现在调用日志和输出中,用于同步调用。Rust 运行时系统客户端还会将错误写入日志。默认情况下,错误会显示在 Amazon CloudWatch 日志中。本页将演示如何在 Lambda 函数的输出中返回错误。

创建返回错误的函数

以下代码示例显示了返回错误的 Lambda 函数。Rust 运行时系统会直接处理此错误。

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 }

此代码将返回以下错误负载:

{ "errorType": "&alloc::string::String", "errorMessage": "something went wrong!" }

有关更高级的错误处理示例,请参阅Amazon实验室 GitHub 存储库中的示例应用程序