gpt4 book ai didi

java - 即使抛出异常,Lambda 返回 200

转载 作者:行者123 更新时间:2023-12-02 10:00:56 27 4
gpt4 key购买 nike

我在 AWS Lambda 中部署了以下代码:

public class MyTrigger implements RequestHandler<Request, Void> {

private final Service service = new Service();

@Override
public Void handleRequest(Request request, Context context) {
service.process(request);
return null;
}
}

并且 .process() 方法抛出 IllegalStateException

使用以下代码“手动”触发此 lambda:

public class LambdaUtils {

private static final Logger log = LoggerFactory.getLogger(LambdaUtils.class);

public static Integer invoke(String functionName, String payload) {
log.info("Invoking lambda {} with payload {}", functionName, payload);
final AWSLambdaAsync lambdaClient = AWSLambdaAsyncClientBuilder.standard().withRegion(Regions.EU_WEST_1).build();
final InvokeRequest request = new InvokeRequest();
request.withFunctionName(functionName).withPayload(payload);
final InvokeResult invokeResult = lambdaClient.invoke(request);
final Integer statusCode = invokeResult.getStatusCode();
log.info("Invoked lambda. Got status code {} and payload {}", statusCode, StandardCharsets.UTF_8.decode(invokeResult.getPayload()).toString());
return statusCode;
}
}

问题是,即使 lambda 内部抛出异常,状态代码仍然是 200:调用了 lambda。获得状态代码 200 和负载 [... stacktrace ...]

我使用以下依赖项来创建客户端并执行调用:

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-lambda</artifactId>
<version>1.11.297</version>
</dependency>

最佳答案

200 与实际的 lambda 结果或 lambda 本身的实际调用无关。如果您基本上得到 200 以外的任何值,则意味着 lambda 调用无法被接受,这意味着您的 lambda 根本不会被调用。

来自AWS :

The status code in the API response doesn't reflect function errors. Error codes are reserved for errors that prevent your function from executing, such as permissions errors, limit errors, or issues with your function's code and configuration. For example, Lambda returns TooManyRequestsException if executing the function would cause you to exceed a concurrency limit at either the account level (ConcurrentInvocationLimitExceeded) or function level (ReservedFunctionConcurrentInvocationLimitExceeded).

关于java - 即使抛出异常,Lambda 返回 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55636686/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com