Merge pull request #125 from f/master

Show backtrace in 500 statuses using `inspect_with_backtrace`
This commit is contained in:
Serdar Dogruyol 2016-03-08 13:54:03 +02:00
commit 5d1c4859d7
2 changed files with 15 additions and 5 deletions

View file

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

View file

@ -46,7 +46,13 @@ def render_404(context)
end end
# Template for 500 Internal Server Error # 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 template = <<-HTML
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
@ -55,11 +61,14 @@ def render_500(context, ex)
body { text-align:center;font-family:helvetica,arial;font-size:22px; body { text-align:center;font-family:helvetica,arial;font-size:22px;
color:#888;margin:20px} color:#888;margin:20px}
#c {margin:0 auto;width:500px;text-align:left} #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> </style>
</head> </head>
<body> <body>
<h2>Kemal has encountered an error. (500)</h2> <h2>Kemal has encountered an error. (500)</h2>
<p>#{ex}</p> #{message}
</body> </body>
</html> </html>
HTML HTML