kemal/src/kemal/common_exception_handler.cr

28 lines
996 B
Crystal
Raw Normal View History

2016-05-05 19:35:36 +00:00
module Kemal
class CommonExceptionHandler < HTTP::Handler
INSTANCE = new
2016-02-14 10:43:25 +00:00
2016-05-05 19:35:36 +00:00
def call(context)
begin
call_next(context)
2016-05-05 20:12:17 +00:00
rescue Kemal::Exceptions::RouteNotFound
context.response.content_type = "text/html"
context.response.status_code = 404
context.response.print Kemal.config.error_handlers[404].call(context)
return context
2016-05-05 20:12:17 +00:00
rescue Kemal::Exceptions::CustomException
status_code = context.response.status_code
if Kemal.config.error_handlers.has_key?(status_code)
context.response.print Kemal.config.error_handlers[status_code].call(context)
return context
end
rescue ex : Exception
2016-05-05 19:35:36 +00:00
context.response.content_type = "text/html"
2016-05-05 20:12:17 +00:00
Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace}\n")
2016-05-05 19:35:36 +00:00
verbosity = Kemal.config.env == "production" ? false : true
2016-05-05 20:12:17 +00:00
return render_500(context, ex.inspect_with_backtrace, verbosity)
2016-05-05 19:35:36 +00:00
end
2016-02-14 10:43:25 +00:00
end
end
end