From 08c3ad359a439ea139da1830c66755bf6c72d34f Mon Sep 17 00:00:00 2001 From: Andy Steed Date: Tue, 3 Feb 2026 15:51:31 -0800 Subject: [PATCH 1/3] fix: generalize the http status code returned to nginx when required --- src/lua/api-gateway/validation/validator.lua | 21 +++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/lua/api-gateway/validation/validator.lua b/src/lua/api-gateway/validation/validator.lua index da39172..e0950e2 100644 --- a/src/lua/api-gateway/validation/validator.lua +++ b/src/lua/api-gateway/validation/validator.lua @@ -255,6 +255,25 @@ function BaseValidator:executeTtl(key) end end +-- converts a response status to a valid HTTP status code +function BaseValidator:convertToValidHttpStatusCode(response_status) + response_status = tonumber(response_status) + if response_status == nil then + return 500 + end + if (response_status >= 100 and response_status <= 599) then + return response_status + end + + local http_code_str = string.sub(tostring(response_status), 1, 3) + local http_code_number = tonumber(http_code_str) + if http_code_number ~= nil and http_code_number >= 100 and http_code_number <= 599 then + return http_code_number + end + + ngx.log(ngx.DEBUG, "Status code: ", tostring(response_status), " is not in a valid HTTP Status Code format") + return 500 +end -- generic exit function for a validator -- function BaseValidator:exitFn(status, resp_body) @@ -269,7 +288,7 @@ function BaseValidator:exitFn(status, resp_body) end end - ngx.status = status + ngx.status = self:convertToValidHttpStatusCode(status) if (ngx.null ~= resp_body) then ngx.say(resp_body) From 705524b28394fa5d3f49e3bb6873303583cef46f Mon Sep 17 00:00:00 2001 From: Andy Steed Date: Tue, 3 Feb 2026 20:45:56 -0800 Subject: [PATCH 2/3] fix: set application/json for response bodies --- src/lua/api-gateway/validation/validator.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lua/api-gateway/validation/validator.lua b/src/lua/api-gateway/validation/validator.lua index e0950e2..7fdc0b3 100644 --- a/src/lua/api-gateway/validation/validator.lua +++ b/src/lua/api-gateway/validation/validator.lua @@ -291,6 +291,7 @@ function BaseValidator:exitFn(status, resp_body) ngx.status = self:convertToValidHttpStatusCode(status) if (ngx.null ~= resp_body) then + ngx.header["Content-Type"] = "application/json" ngx.say(resp_body) end From ba18968283523498edc3fcb8dfe9fad91aca1a52 Mon Sep 17 00:00:00 2001 From: Andy Steed Date: Wed, 11 Feb 2026 15:38:54 -0800 Subject: [PATCH 3/3] fix: handle extended status --- .../validation/validatorsHandlerErrorDecorator.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lua/api-gateway/validation/validatorsHandlerErrorDecorator.lua b/src/lua/api-gateway/validation/validatorsHandlerErrorDecorator.lua index dd2528b..dbb7c55 100644 --- a/src/lua/api-gateway/validation/validatorsHandlerErrorDecorator.lua +++ b/src/lua/api-gateway/validation/validatorsHandlerErrorDecorator.lua @@ -109,6 +109,15 @@ function ValidatorHandlerErrorDecorator:decorateResponse(response_status, respon response_status = tonumber(response_status) local o = getResponsesTemplate()[response_status] + -- If no match by status code (e.g. status was converted from an extended error_code like 401013 to 401), + -- try to extract the error_code from the response body and look up by that instead. + if (o == nil and response_body ~= nil and #response_body > 0 and response_body ~= "nil\n") then + local ok, json_body = pcall(cjson.decode, response_body) + if ok and json_body and json_body.error_code then + o = getResponsesTemplate()[tonumber(json_body.error_code)] + end + end + if (o ~= nil) then ngx.status = self:convertToValidHttpStatusCode(o.http_status) -- NOTE: assumption: for the moment if it's custom, then it's application/json