简体   繁体   中英

How to enable error reporting when running a rails app in production mode?

I am working in a rails app. I can able to see the below screen in my production server whenever there is an error. (Text : We're sorry, but something went wrong.)

How do i enable error reporting in my server to see the actual error? Do i have to do any config change in Rails app or enable error reporting in my server.

在此输入图像描述

Note : My server is Ngnix.

If you add this to your config/environments/production.rb :

config.consider_all_requests_local = true

You will see the same errors as in development, but it will also disable the caching for the production app etc.

I would rather suggest you look into /log/production.log . The same errors are posted there as are normally shown on screen.

For error reporting, there's a handy gem called exception_notification which sends you a mail every time an exception happens in your application.

If you also want to get detailed and fully customizable error reporting on a page, you'd have to do a little coding. Here's what I use (I'd give you a link to where I've taken this from, but I can't find it any longer :( )

  1. Define an ErrorsController like this:

     class ErrorsController < ApplicationController ERRORS = [ :internal_server_error, :not_found, :unprocessable_entity, :unauthorized ].freeze ERRORS.each do |e| define_method e do respond_to do |format| format.html { render e, :status => e } format.any { head e } end end end 

    end

  2. Create an initializer to hook the controller into the Rails exception handling chain:

     require 'action_dispatch/middleware/show_exceptions' module ActionDispatch class ShowExceptions private def render_exception_with_template(env, exception) env['exception'] = exception body = ErrorsController.action(rescue_responses[exception.class.name]).call(env) log_error(exception) env.delete 'exception' body rescue Exception => e log_error(e) render_exception_without_template(env, exception) end alias_method_chain :render_exception, :template end end 
  3. Create a view for each of the errors in ErrorsController::ERRORS (ie app/views/errors/not_found.html.erb etc).

I don't claim this to be the best technique, but it has served me well so far (you can easily add pages for new errors, customize how and what you want displayed for each error separately).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM