Error jumping to uniform hint error page encountered in Rails _ruby topic

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.