Rust 中的 Lambda 函数错误
注意
Rust 运行时系统客户端
您的代码引发错误时,Lambda 将会生成错误的 JSON 表示形式。此错误文档会出现在调用日志和输出中,用于同步调用。Rust 运行时系统客户端
创建返回错误的函数
以下代码示例显示了返回错误的 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 存储库中的示例应用程序