fix: when a request caused a 500 error, the status was converted to 405 (#4696)

It happened when this request is not a GET or HEAD request.

Signed-off-by: spacewander <spacewanderlzx@gmail.com>
diff --git a/.gitignore b/.gitignore
index 3697d8d..c06fa2c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -77,4 +77,3 @@
 # release tar package
 *.tgz
 release/*
-html/*
diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua
index 0816fae..69dcfe7 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -352,7 +352,9 @@
 
         location @50x.html {
             set $from_error_page 'true';
-            try_files /50x.html $uri;
+            content_by_lua_block {
+                require("apisix.error_handling").handle_500()
+            }
         }
     }
     {% end %}
@@ -434,7 +436,9 @@
 
         location @50x.html {
             set $from_error_page 'true';
-            try_files /50x.html $uri;
+            content_by_lua_block {
+                require("apisix.error_handling").handle_500()
+            }
         }
     }
     {% end %}
@@ -683,7 +687,9 @@
 
         location @50x.html {
             set $from_error_page 'true';
-            try_files /50x.html $uri;
+            content_by_lua_block {
+                require("apisix.error_handling").handle_500()
+            }
             header_filter_by_lua_block {
                 apisix.http_header_filter_phase()
             }
diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua
index 73166a6..4c1c46a 100644
--- a/apisix/cli/ops.lua
+++ b/apisix/cli/ops.lua
@@ -19,7 +19,6 @@
 local util = require("apisix.cli.util")
 local file = require("apisix.cli.file")
 local ngx_tpl = require("apisix.cli.ngx_tpl")
-local html_page = require("apisix.cli.html_page")
 local profile = require("apisix.core.profile")
 local template = require("resty.template")
 local argparse = require("argparse")
@@ -683,14 +682,6 @@
     if not ok then
         util.die("failed to update nginx.conf: ", err, "\n")
     end
-
-    local cmd_html = "mkdir -p " .. env.apisix_home .. "/html"
-    util.execute_cmd(cmd_html)
-
-    local ok, err = util.write_file(env.apisix_home .. "/html/50x.html", html_page)
-    if not ok then
-        util.die("failed to write 50x.html: ", err, "\n")
-    end
 end
 
 
diff --git a/apisix/cli/html_page.lua b/apisix/error_handling.lua
similarity index 84%
rename from apisix/cli/html_page.lua
rename to apisix/error_handling.lua
index f262ad8..524c46b 100644
--- a/apisix/cli/html_page.lua
+++ b/apisix/error_handling.lua
@@ -14,9 +14,12 @@
 -- See the License for the specific language governing permissions and
 -- limitations under the License.
 --
+local core = require("apisix.core")
+local ngx = ngx
 
-return [=[
-<!DOCTYPE html>
+
+local _M = {}
+local page_500 = [[<!DOCTYPE html>
 <html>
 <head>
 <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
@@ -35,5 +38,13 @@
 <p>You can report issue to <a href="https://github.com/apache/apisix/issues">APISIX</a></p>
 <p><em>Faithfully yours, <a href="https://apisix.apache.org/">APISIX</a>.</em></p>
 </body>
-</html>
-]=]
+</html>]]
+
+
+function _M.handle_500()
+    core.response.set_header("Content-Type", "text/html")
+    ngx.say(page_500)
+end
+
+
+return _M
diff --git a/t/APISIX.pm b/t/APISIX.pm
index e1c5557..4c615a8 100644
--- a/t/APISIX.pm
+++ b/t/APISIX.pm
@@ -89,7 +89,6 @@
 my $ssl_ecc_key = read_file("t/certs/apisix_ecc.key");
 my $test2_crt = read_file("t/certs/test2.crt");
 my $test2_key = read_file("t/certs/test2.key");
-my $test_50x_html = read_file("t/error_page/50x.html");
 $user_yaml_config = <<_EOC_;
 apisix:
   node_listen: 1984
@@ -541,7 +540,9 @@
 
         location \@50x.html {
             set \$from_error_page 'true';
-            try_files /50x.html \$uri;
+            content_by_lua_block {
+                require("apisix.error_handling").handle_500()
+            }
             header_filter_by_lua_block {
                 apisix.http_header_filter_phase()
             }
@@ -627,7 +628,9 @@
 
         location \@50x.html {
             set \$from_error_page 'true';
-            try_files /50x.html \$uri;
+            content_by_lua_block {
+                require("apisix.error_handling").handle_500()
+            }
             header_filter_by_lua_block {
                 apisix.http_header_filter_phase()
             }
@@ -774,8 +777,6 @@
 $test2_crt
 >>> ../conf/cert/test2.key
 $test2_key
->>> 50x.html
-$test_50x_html
 $user_apisix_yaml
 _EOC_
 
diff --git a/t/error_page/50x.html b/t/error_page/50x.html
deleted file mode 100644
index f3b9e20..0000000
--- a/t/error_page/50x.html
+++ /dev/null
@@ -1,38 +0,0 @@
-<!--
-#
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements.  See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.
-# The ASF licenses this file to You under the Apache License, Version 2.0
-# (the "License"); you may not use this file except in compliance with
-# the License.  You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
--->
-<!DOCTYPE html>
-<html>
-<head>
-<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
-<meta content="utf-8" http-equiv="encoding">
-<title>500 Internal Server Error</title>
-<style>
-    body {
-        width: 35em;
-        margin: 0 auto;
-        font-family: Tahoma, Verdana, Arial, sans-serif;
-    }
-</style>
-</head>
-<body>
-<h1>An error occurred.</h1>
-<p>You can report issue to <a href="https://github.com/apache/apisix/issues">APISIX</a></p>
-<p><em>Faithfully yours, <a href="https://apisix.apache.org/">APISIX</a>.</em></p>
-</body>
-</html>
diff --git a/t/error_page/error_page.t b/t/error_page/error_page.t
index 6eb0fa7..70cc34c 100644
--- a/t/error_page/error_page.t
+++ b/t/error_page/error_page.t
@@ -118,7 +118,19 @@
 
 
 
-=== TEST 7: delete route(id: 1)
+=== TEST 7: test apisix with internal error code 500, method isn't GET or HEAD
+--- request
+POST /hello
+123
+--- more_headers
+X-Test-Status: 500
+--- error_code: 500
+--- response_body_like
+.*apisix.apache.org.*
+
+
+
+=== TEST 8: delete route(id: 1)
 --- config
     location /t {
         content_by_lua_block {
@@ -142,7 +154,7 @@
 
 
 
-=== TEST 8: set route which upstream is blocking
+=== TEST 9: set route which upstream is blocking
 --- config
     location /t {
         content_by_lua_block {
@@ -175,7 +187,7 @@
 
 
 
-=== TEST 9: client abort
+=== TEST 10: client abort
 --- request
 GET /mysleep?seconds=3
 --- abort