Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

Note : My server is Ngnix.

like image 406
balanv Avatar asked Jul 25 '12 12:07

balanv


2 Answers

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.

like image 145
Tigraine Avatar answered Nov 09 '22 23:11

Tigraine


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 (i.e. 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).

like image 38
HargrimmTheBleak Avatar answered Nov 09 '22 22:11

HargrimmTheBleak