Skip to content

Commit

Permalink
fix: the phases after proxy should run when 500 happens before proxy (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander authored and Liu-Junlin committed Nov 4, 2022
1 parent e035a2d commit 4dd2b39
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
8 changes: 4 additions & 4 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ function _M.http_access_phase()
api_ctx.route_id = route.value.id
api_ctx.route_name = route.value.name

local ref = ctxdump.stash_ngx_ctx()
core.log.info("stash ngx ctx: ", ref)
ngx_var.ctx_ref = ref

-- run global rule
plugin.run_global_rules(api_ctx, router.global_rules, nil)

Expand Down Expand Up @@ -546,10 +550,6 @@ function _M.http_access_phase()
-- run the before_proxy method in access phase first to avoid always reinit request
common_phase("before_proxy")

local ref = ctxdump.stash_ngx_ctx()
core.log.info("stash ngx ctx: ", ref)
ngx_var.ctx_ref = ref

local up_scheme = api_ctx.upstream_scheme
if up_scheme == "grpcs" or up_scheme == "grpc" then
return ngx.exec("@grpc_pass")
Expand Down
1 change: 1 addition & 0 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ _EOC_
more_clear_headers Date;
}
# this configuration is needed as error_page is configured in http block
location \@50x.html {
set \$from_error_page 'true';
content_by_lua_block {
Expand Down
55 changes: 55 additions & 0 deletions t/error_page/error_page.t
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,58 @@ qr/(stash|fetch) ngx ctx/
--- grep_error_log_out
stash ngx ctx
fetch ngx ctx



=== TEST 11: check if the phases after proxy are run when 500 happens before proxy
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"serverless-post-function": {
"functions" : ["return function() if ngx.var.http_x_test_status ~= nil then;ngx.exit(tonumber(ngx.var.http_x_test_status));end;end"]
},
"serverless-pre-function": {
"phase": "log",
"functions" : ["return function() ngx.log(ngx.WARN, 'run log phase in error_page') end"]
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/*"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 12: hit
--- request
GET /hello
--- more_headers
X-Test-Status: 500
--- error_code: 500
--- response_body_like
.*apisix.apache.org.*
--- error_log
run log phase in error_page

0 comments on commit 4dd2b39

Please sign in to comment.