mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(apigatewayv2): answer the Lambda proxy results an HTTP API cannot read with 500 (#5985)
* fix(apigatewayv2): answer the Lambda proxy results an HTTP API cannot read with 500
Measured on HTTP APIs (eu-west-1), with payload formats 1.0 and 2.0:
a function that raises is answered 500 with {"message":"Internal Server
Error"}, not the 502 Floci sent. The same 500 answers what API Gateway
cannot read as a response: in format 1.0 a result that is not an object
with a statusCode (a result without one, a string, a number, null), and
in either format a statusCode that is not a whole number from 100 to 599
(99, 600, 201.5, "abc"); a statusCode given as text that spells one,
"201", is taken, and headers that are not an object are left out.
Format 2.0 keeps inferring a 200 for a result without a statusCode.
Floci answered a format 1.0 result without a statusCode with 200 and
its headers and body, as HttpApiLambdaProxyResponseInferenceIntegrationTest
pinned since #5153; AWS answers 500, and that test and the unit tests
that pinned 502 now expect what AWS answers. A REST API's Lambda proxy
keeps its own 502, untouched.
The HTTP API's generic error responses come from one helper, which the
invocation failures from #5935 (500, or 503 when Lambda throttles) use
too.
* test(compat): skip the HTTP API proxy cases unless the API reaches Lambda
Review of #5985: the test checked only that Lambda can be invoked, not
that API Gateway dispatches to it. It now sends a readable result through
the API first, as ApiGatewayV2ExecuteTest does, and skips the cases when
that does not come back.
This commit is contained in:
1 parent
241806a4f3
commit
49d2d74f3f
6 files changed
+256
-19
No files matched your search
+33
-7
@@ -1586,9 +1586,9 @@ public class ApiGatewayExecuteController {
|
||||
// response rules (inference and cookies). An HTTP API integration can still use format 1.0.
|
||||
Response buildProxyResponse(InvokeResult result, boolean httpApi, boolean payloadV2) {
|
||||
if (httpApi && result.getFunctionError() != null) {
|
||||
// An HTTP API never relays the error payload: the client gets AWS's generic message.
|
||||
return Response.status(502).entity(jsonMessage("Internal Server Error"))
|
||||
.type(MediaType.APPLICATION_JSON).build();
|
||||
// An HTTP API never relays the error payload: the client gets AWS's generic message, as
|
||||
// a 500 (measured, both payload formats, eu-west-1, 2026-10-19).
|
||||
return httpApiError(500, "Internal Server Error");
|
||||
}
|
||||
if (result.getPayload() == null || result.getPayload().length == 0) {
|
||||
return Response.status(result.getFunctionError() != null ? 502 : result.getStatusCode()).build();
|
||||
@@ -1605,7 +1605,14 @@ public class ApiGatewayExecuteController {
|
||||
: result.getPayload();
|
||||
return Response.status(200).entity(inferredBody).type(MediaType.APPLICATION_JSON).build();
|
||||
}
|
||||
int statusCode = node.path("statusCode").asInt(200);
|
||||
if (httpApi && httpApiStatusCode(node) == null) {
|
||||
// Measured (eu-west-1, 2026-10-19): an HTTP API answers a response it cannot read
|
||||
// with 500 and its generic message, in format 1.0 a result that is not an object with
|
||||
// a statusCode, and in either format a statusCode that is not a whole number from 100
|
||||
// to 599, though text spelling one ("201") is taken.
|
||||
return httpApiError(500, "Internal Server Error");
|
||||
}
|
||||
int statusCode = httpApi ? httpApiStatusCode(node) : node.path("statusCode").asInt(200);
|
||||
if (result.getFunctionError() != null && !node.has("statusCode")) statusCode = 502;
|
||||
|
||||
Response.ResponseBuilder builder = Response.status(statusCode);
|
||||
@@ -1644,6 +1651,27 @@ public class ApiGatewayExecuteController {
|
||||
}
|
||||
}
|
||||
|
||||
/** The status and generic message an HTTP API answers with when its Lambda integration fails. */
|
||||
private Response httpApiError(int status, String message) {
|
||||
return Response.status(status).entity(jsonMessage(message)).type(MediaType.APPLICATION_JSON).build();
|
||||
}
|
||||
|
||||
private static final Pattern WHOLE_STATUS = Pattern.compile("\\d{1,3}");
|
||||
|
||||
/**
|
||||
* The status an HTTP API takes from a Lambda proxy response, or null when it cannot read one: a
|
||||
* whole number from 100 to 599, or text that spells one.
|
||||
*/
|
||||
private static Integer httpApiStatusCode(JsonNode response) {
|
||||
JsonNode status = response.path("statusCode");
|
||||
String text = status.isIntegralNumber() ? status.asText() : status.isTextual() ? status.textValue() : "";
|
||||
if (!WHOLE_STATUS.matcher(text).matches()) {
|
||||
return null;
|
||||
}
|
||||
int code = Integer.parseInt(text);
|
||||
return code >= 100 && code <= 599 ? code : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP header names are case-insensitive on the wire (RFC 7230 §3.2), and Lambda proxy
|
||||
* integrations commonly return lowercased names (e.g. the AWS Lambda Web Adapter emits
|
||||
@@ -2776,9 +2804,7 @@ public class ApiGatewayExecuteController {
|
||||
// function that is gone, or one API Gateway may not invoke).
|
||||
LOG.debugv("execute-api v2: invoking {0} failed: {1}", functionName, e.getMessage());
|
||||
boolean throttled = e.getHttpStatus() == 429;
|
||||
return Response.status(throttled ? 503 : 500)
|
||||
.entity(jsonMessage(throttled ? "Service Unavailable" : "Internal Server Error"))
|
||||
.type(MediaType.APPLICATION_JSON).build();
|
||||
return throttled ? httpApiError(503, "Service Unavailable") : httpApiError(500, "Internal Server Error");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user