application controller: clean up exception handling.

* Simplify code.
* Show backtraces for all users, not just builders.
* Show backtraces only for unexpected server errors (status 5xx), not
  for normal client errors (status 4xx).
* Log expected errors at info level (reduce noise in production logs).
This commit is contained in:
evazion
2019-08-13 21:30:20 -05:00
parent 84d311f366
commit e70cae457d
7 changed files with 32 additions and 40 deletions

View File

@@ -73,21 +73,19 @@ class ApplicationController < ActionController::Base
end
def rescue_exception(exception)
@exception = exception
case exception
when ActiveRecord::QueryCanceled
render_error_page(500, exception, message: "The database timed out running your query.")
when ActionController::BadRequest
render_error_page(400, exception, expected: true)
render_error_page(400, exception)
when ActiveRecord::RecordNotFound
render_error_page(404, exception, message: "That record was not found", expected: true)
render_error_page(404, exception, message: "That record was not found.")
when ActionController::RoutingError
render_error_page(405, exception, expected: true)
render_error_page(405, exception)
when ActionController::UnknownFormat, ActionView::MissingTemplate
render_error_page(406, exception, message: "#{request.format.to_s} is not a supported format for this page", html: true, expected: true)
render_error_page(406, exception, message: "#{request.format.to_s} is not a supported format for this page", format: :html)
when Danbooru::Paginator::PaginationError
render_error_page(410, exception, expected: true)
render_error_page(410, exception)
when NotImplementedError
render_error_page(501, exception, message: "This feature isn't available: #{exception.message}")
when PG::ConnectionBad
@@ -97,16 +95,15 @@ class ApplicationController < ActionController::Base
end
end
def render_error_page(status, exception, message: exception.message, html: false, expected: false)
@error_message = message
def render_error_page(status, exception, message: exception.message, format: request.format.symbol)
@exception = exception
@expected = status < 500
@message = message.encode("utf-8", { invalid: :replace, undef: :replace })
@backtrace = Rails.backtrace_cleaner.clean(@exception.backtrace)
format = :html unless format.in?(%i[html json xml js atom])
DanbooruLogger.log(exception, expected: expected)
if html || !request.format.symbol.in?(%i[html json xml js atom])
render template: "static/error.html", status: status
else
render template: "static/error", status: status
end
DanbooruLogger.log(@exception, expected: @expected)
render "static/error", status: status, formats: format
end
def authentication_failed