Show backtrace in 500 statuses using inspect_with_backtrace

This commit is contained in:
Fatih Kadir Akın 2016-03-08 11:03:25 +02:00
parent a194bebe31
commit 8402f06bc3
2 changed files with 15 additions and 5 deletions

View file

@ -5,11 +5,12 @@ class Kemal::CommonErrorHandler < HTTP::Handler
begin
call_next context
rescue ex : Kemal::Exceptions::RouteNotFound
Kemal.config.logger.write("Exception: #{ex.to_s}\n")
Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace.colorize(:red)}\n")
return render_404(context)
rescue ex
Kemal.config.logger.write("Exception: #{ex.to_s}\n")
return render_500(context, ex.to_s)
Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace.colorize(:red)}\n")
verbosity = Kemal.config.env == "production" ? false : true
return render_500(context, ex.inspect_with_backtrace, verbosity)
end
end
end

View file

@ -46,7 +46,13 @@ def render_404(context)
end
# Template for 500 Internal Server Error
def render_500(context, ex)
def render_500(context, backtrace, verbosity)
message = if verbosity
"<pre>#{backtrace}</pre>"
else
"<p>Something wrong with the server :(</p>"
end
template = <<-HTML
<!DOCTYPE html>
<html>
@ -55,11 +61,14 @@ def render_500(context, ex)
body { text-align:center;font-family:helvetica,arial;font-size:22px;
color:#888;margin:20px}
#c {margin:0 auto;width:500px;text-align:left}
pre {text-align:left;font-size:14px;color:#fff;background-color:#222;
font-family:Operator,"Source Code Pro",Menlo,Monaco,Inconsolata,monospace;
line-height:1.5;padding:10px;border-radius:2px;overflow:scroll}
</style>
</head>
<body>
<h2>Kemal has encountered an error. (500)</h2>
<p>#{ex}</p>
#{message}
</body>
</html>
HTML