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 :( )
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
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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With