An iterative development of the site will inevitably exist bugs, when the customer experience is very bad, in order to solve this problem, can be generated when class error, trigger jump to the unified Tip page, and to the developer to send an error message, improve the test capability and user experience. The following is the core approach; The following code is added to the Applicationcontroller, and the class error for different rails versions changes slightly.
Copy Code code as follows:
Ar_error_classes = [Activerecord::recordnotfound, Activerecord::statementinvalid]
Error_classes = [Nameerror, Nomethoderror, RuntimeError,
Actionview::templateerror,
Activerecord::staleobjecterror, Actioncontroller::routingerror,
Actioncontroller::unknowncontroller, Abstractcontroller::actionnotfound,
Actioncontroller::methodnotallowed, Actioncontroller::invalidauthenticitytoken]
Access_denied_classes = [Cancan::accessdenied]
If Rails.env.production?
Rescue_from *ar_error_classes,: with =>: Render_ar_error
Rescue_from *error_classes,: with =>: Render_error
Rescue_from *access_denied_classes,: with =>: render_access_denied
End
#called by the last route matching unmatched routes. Raises routingerror which is rescued from the same way as other exceptions.
#备注rails3.1 Actioncontroller::routingerror The following code is finally added to the routes.rb to catch up.
#rails3下: Match ' *unmatched_route ',: to => ' application#raise_not_found! '
#rails4下: Get ' *unmatched_route ',: to => ' application#raise_not_found! '
def raise_not_found!
Raise Actioncontroller::routingerror.new ("No route matches #{params[:unmatched_route]}")
End
def render_ar_error (Exception)
Case exception
When *ar_error_classes then Exception_class = exception.class.to_s
else Exception_class = ' exception '
End
Send_error_email (Exception, Exception_class)
End
def render_error (Exception)
Case exception
When *error_classes then Exception_class = exception.class.to_s
else Exception_class = ' exception '
End
Send_error_email (Exception, Exception_class)
End
def render_access_denied (Exception)
Case exception
When *access_denied_classes then Exception_class = exception.class.to_s
else Exception_class = "Exception"
End
Send_error_email (Exception, Exception_class)
End