server: add "internal server error" handler

This commit is contained in:
0x1eef 2023-06-27 15:22:18 -03:00
parent 63d5206a10
commit 786700e6ea
2 changed files with 14 additions and 0 deletions

View file

@ -20,6 +20,9 @@ class Server::Dir
rescue Errno::ENOENT
body = "The requested URL was not found"
[404, {"content-length" => body.bytesize, "content-type" => "text/plain"}, [body]]
rescue => ex
body = "Internal server error (#{ex.class})"
[500, {"content-length" => body.bytesize, "content-type" => "text/plain"}, [body]]
end
private

View file

@ -43,6 +43,17 @@ class ServerDirTest < Test::Unit::TestCase
last_response.content_length
end
def test_internal_server_error
def app.read(path) raise RuntimeError, "test" end
get "/"
assert_equal 500, last_response.status
assert_equal "text/plain", last_response.content_type
assert_equal "Internal server error (RuntimeError)".bytesize,
last_response.content_length
assert_equal "Internal server error (RuntimeError)",
last_response.body
end
def test_permission_denied
File.chmod 0, "./test/webroot/permission_denied.html"
get "/permission_denied.html"