mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
Add config to customize the default error message
Add a lowlevel_error_handler, so we can customize the default error message. example: ``` lowlevel_error_handler do [302, {'Content-Type' => 'text', 'Location' => 'foo.html'}, ['302 found']] end ``` [fix #458]
This commit is contained in:
parent
f8d49e86ab
commit
e424eaf052
5 changed files with 45 additions and 1 deletions
|
@ -327,6 +327,15 @@ module Puma
|
||||||
def preload_app!(answer=true)
|
def preload_app!(answer=true)
|
||||||
@options[:preload_app] = answer
|
@options[:preload_app] = answer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Use +obj+ or +block+ as the low lever error handler. This allows a config file to
|
||||||
|
# change the default error on the server.
|
||||||
|
#
|
||||||
|
def lowlevel_error_handler(obj=nil, &block)
|
||||||
|
obj ||= block
|
||||||
|
raise "Provide either a #call'able or a block" unless obj
|
||||||
|
@options[:lowlevel_error_handler] = obj
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -707,6 +707,10 @@ module Puma
|
||||||
# A fallback rack response if +@app+ raises as exception.
|
# A fallback rack response if +@app+ raises as exception.
|
||||||
#
|
#
|
||||||
def lowlevel_error(e)
|
def lowlevel_error(e)
|
||||||
|
if handler = @options[:lowlevel_error_handler]
|
||||||
|
return handler.call(e)
|
||||||
|
end
|
||||||
|
|
||||||
if @leak_stack_on_error
|
if @leak_stack_on_error
|
||||||
[500, {}, ["Puma caught this error: #{e.message} (#{e.class})\n#{e.backtrace.join("\n")}"]]
|
[500, {}, ["Puma caught this error: #{e.message} (#{e.class})\n#{e.backtrace.join("\n")}"]]
|
||||||
else
|
else
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
app do |env|
|
app do |env|
|
||||||
[200, {}, ["embedded app"]]
|
[200, {}, ["embedded app"]]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
lowlevel_error_handler do |err|
|
||||||
|
[200, {}, ["error page"]]
|
||||||
|
end
|
||||||
|
|
|
@ -13,4 +13,14 @@ class TestConfigFile < Test::Unit::TestCase
|
||||||
|
|
||||||
assert_equal [200, {}, ["embedded app"]], app.call({})
|
assert_equal [200, {}, ["embedded app"]], app.call({})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_lowleve_error_handler_DSL
|
||||||
|
opts = { :config_file => "test/config/app.rb" }
|
||||||
|
conf = Puma::Configuration.new opts
|
||||||
|
conf.load
|
||||||
|
|
||||||
|
app = conf.options[:lowlevel_error_handler]
|
||||||
|
|
||||||
|
assert_equal [200, {}, ["error page"]], app.call({})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -220,6 +220,23 @@ class TestPumaServer < Test::Unit::TestCase
|
||||||
data = sock.read
|
data = sock.read
|
||||||
|
|
||||||
assert_not_match(/don't leak me bro/, data)
|
assert_not_match(/don't leak me bro/, data)
|
||||||
|
assert_match(/HTTP\/1.0 500 Internal Server Error/, data)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_prints_custom_error
|
||||||
|
@events = Puma::Events.strings
|
||||||
|
re = lambda { |err| [302, {'Content-Type' => 'text', 'Location' => 'foo.html'}, ['302 found']] }
|
||||||
|
@server = Puma::Server.new @app, @events, {lowlevel_error_handler: re}
|
||||||
|
|
||||||
|
@server.app = proc { |e| raise "don't leak me bro" }
|
||||||
|
@server.add_tcp_listener @host, @port
|
||||||
|
@server.run
|
||||||
|
|
||||||
|
sock = TCPSocket.new @host, @port
|
||||||
|
sock << "GET / HTTP/1.0\r\n\r\n"
|
||||||
|
|
||||||
|
data = sock.read
|
||||||
|
assert_match(/HTTP\/1.0 302 Found/, data)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_custom_http_codes_10
|
def test_custom_http_codes_10
|
||||||
|
|
Loading…
Reference in a new issue