Rails full error page not showing for view errors - ruby-on-rails

I have a view named new.html.erb with the following code:
<%= some_non_existent_thing.imaginary_method %>
Now, I only see one simple 500 error page, like that:
500 Internal Server Error
If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.
Shouldn't I see a pretty formatted page with some information about the exception?
I'm not sure I miss something here, but I believe rails used to show the full error page in development environment when there is something wrong in the view.

Are you sure that you are running the development environment? Check that RAILS_ENV=development or that you are running rails server -e development.
Then check your development.rb file, you should have the following line in it
config.consider_all_requests_local = true

If you happen to have an exception inside an exception, Rails has a middleware that catches it and returns a FAILSAFE_RESPONSE with the following copy:
500 Internal Server Error
If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.
A nice way to troubleshoot this is to compare your custom error code with the sample code provided in the Rails 4.2.0 guides.
I'm pointing to that particular version because that whole section was removed in the Rails 5.0.0 guides.
Ideally, you should keep your error views, layout, and controller as free of logic as possible, to avoid running into this issue.

Firstly, as Anton mentions in the answer below, confirm that your config/environments/development.rb has:
config.consider_all_requests_local = true
and that you are running the server in development mode.
I had ensured the above and still observed the error.
This happened in my case, because my logger had some errors. I was using a custom log formatter, which used the String#% method to format the log. % seems to be very buggy and leads to all these weird errors. I figured this one out by adding a debugger line to the controller method and stepping through into the implicit render function call. It hit the bug and reported it as a malformed format string error.
This was the log formatter I was using before, which caused the bugs [I had added it to an initializer file]:
class Logger::SimpleFormatter
def call(severity, time, progname, msg)
"%-7s #{msg}\n" % severity
end
end
These modifications fixed the bug:
class Logger::SimpleFormatter
def call(severity, time, progname, msg)
severity_prefix = "[#{severity}]".ljust(7)
"#{severity_prefix} #{msg}\n"
end
end

This happens if the code is not compilable, for example if you have an if statement missing an end.

Related

How can I get Rails to give me more details on an error it's not logging?

This has been stumping me: Rails is throwing this error, after it's finished rendering my views, but before it gets back to the browser:
ActionView::Template::Error (undefined method `start_with?' for #<Proc:0x00005651bfe017f0>)
And... that's it. There's no stack trace. I get shown the standard 500 "We're sorry, but something went wrong" page, despite having config.consider_all_requests_local = true set. There are no further details either in the terminal or in log/development.log.
I can't find any Procs that it might be complaining about, nor can I find any calls to start_with? that might be the cause; I've gone back through Git history and isolated the issue to one commit (this one, if you want to take a look in detail), but nothing within that commit jumps out as being obvious.
Calling a render layout: false does work, as does simplifying my layouts/application.js down to just a <%= yield %>, which makes me think it might be something in there, however - I made no changes to it or any views at all in the commit in which the issue appeared.
What I'd really like to know is how I can get Rails to give me the stack trace for this error, so I can figure out where it's coming from. If you have any ideas where the bug itself might be, those are more than welcome too.
Drop this in an initializer (proc.rb):
class Proc
def start_with?(*args)
puts caller
end
end

Rails 4.2 Custom Error Page for Sidekiq When Missing ENV

In our app, we have Sidekiq and have modified the sidekiq.rb file to have it require authentication to view the page.
sidekiq.rb
Sidekiq::Web.use(Rack::Auth::Basic) do |user, password|
[user, password] == [ENV['SIDEKIQ_USER'], ENV['SIDEKIQ_PASSWORD']]
end
We want to have an error page set up for whenever the ENVs above are missing it will let us know specifically that the missing ENVs are the issue.
Solution 1: check the Sidekiq web code, open its base controller class and write a before action to render an error page when error happens.
Solution 2: write a conditional route, show the error page when error happens
Solution 3: throw errors in sidekiq.rb, so your server wont startup successfully, you will not need an error page anymore. I think the last one is the best, because this is just internal page to be viewed by few people, and probably only you, its better to find the problem as soon as possible.

Exception_Notification is not aware of RecordNotFound exceptions

I'm trying to use exception_notification for the first time. I watched the Railscast and followed instructions from the author in http://smartinez87.github.io/exception_notification/. Everything seems to work fine with some sort of exceptions but not with others.
I tested and received email error notificatios from my dev environment with errors such as "An ActionView::Template::Error occurred in static_pages#home:". But, there are some Exceptions such as RoutingException and RecordNotFound that are not being catched by ExceptionNotification, and I don't know why, as I have not any rescue_from strategy of any kind in my application_controller.
I'm using rails 3.2.12 and checked the middleware stack array, and I can see ExceptionNotification is just the last one, and it seems that some kind of exceptions does not go their way down the stack, so Exception Notification is not aware of them.
So, the question are: what am i doing wrong? what is the difference between ActionController::RoutingError or ActiveRecord::RecordNotFound which are not catched by ExceptionNotification and ActionView::Template::Error which is catched and causes Exception Notification to send the notification email to my inbox.
Thanks in advance
These exception types are being ignored as part of the default configuration of that gem. See line 25 here: https://github.com/smartinez87/exception_notification/blob/master/lib/exception_notifier.rb which currently reads:
##ignored_exceptions = %w{ActiveRecord::RecordNotFound AbstractController::ActionNotFound ActionController::RoutingError ActionController::UnknownFormat}
You can override this behavior in your environment file (i.e. development.rb, etc).
Example to notify on ALL errors:
config.middleware.use ExceptionNotifier,
ignore_exceptions: []
Example to add RuntimeError to the default ignore list:
config.middleware.use ExceptionNotifier,
ignore_exceptions: ExceptionNotifier.default_ignore_exceptions + [RuntimeError]

Joining separate log to main Rails development log

This is the reverse of the question I have seen several times elsewhere, in which someone wants to see how to create an another, separate Rails log from the main development log. For some reason, my Rails app is logging my DelayedJob gem's activity to a separate log (delayed_job.log), but I want it to log to the main development.log file. I am using the Workless gem and NewRelic as well, should this be potentially relevant (although I experimented on this by removing NewRelic, and the issue still remained).
I'm not clear on how this happened. However, I was having some trouble earlier with seeing SQL insertions and deletions in my log, and another user kindly suggested that I use the following in an initializer file:
if defined?(Rails) && !Rails.env.nil?
logger = Logger.new(STDOUT)
ActiveRecord::Base.logger = logger
ActiveResource::Base.logger = logger
end
Once I did this, I saw the SQL statements, but no longer saw the DelayedJob information in the main development log.
So my question is: How can I make sure that DelayedJob activity logs to the main development log? I don't mind if it also logs to a separate log, but the important thing is that I see its activity in my Mac's console.
Please let me know if you'd like more code from my app - I'd be happy to provide it. Much thanks from a Rails newbie.
Try adding the following line to config/initializers/delayed_job_config.rb
Delayed::Worker.logger = Logger.new(STDOUT)
I finally got this to work. All thanks to Seamus Abshere's answer to the question here. I put what he posted below in an initializer file. This got delayed_job to log to my development.rb file (huzzah!).
However, delayed_job still isn't logging into my console (for reasons I still don't understand). I solved that by opening a new console tab and entering tail -f log/development.log.
Different from what Seamus wrote, though, auto-flushing=true is deprecated in Rails 4 and my Heroku app crashed. I resolved this by removing it from my initializer file and placing it in my environments/development.rb file as config.autoflush_log = true. However, I found that neither of the two types of flushing were necessary to make this work.
Here is his code (without the auto-flushing):
file_handle = File.open("log/#{Rails.env}_delayed_jobs.log", (File::WRONLY | File::APPEND | File::CREAT))
# Be paranoid about syncing
file_handle.sync = true
# Hack the existing Rails.logger object to use our new file handle
Rails.logger.instance_variable_set :#log, file_handle
# Calls to Rails.logger go to the same object as Delayed::Worker.logger
Delayed::Worker.logger = Rails.logger
If the above code doesn't work, try replacing Rails.logger with RAILS_DEFAULT_LOGGER.

Rails 3.2.13, 500 error in development with no log

I've got problem with migrating rails 2.x -> 3.2.13
At some point, after fixing a few things, I get Completed 500 Internal Server Error in 76ms without any traceback.
development.rb
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
Why there is no traceback and how to fix this?
you probably solved it but I wanted to share my couple of hours debugging about this issue as it can be very annoying. In short, I was having the same problem - 500 internal server error without any log of the exception thrown. It was happening only for exceptions thrown in the action view templates - any ActionView::Template::Error exception. For example, missing partial, invalid route.
My specific problem was using this ruby statistics module:
http://derrick.pallas.us/ruby-stats/
directly in the initializers directory which works great in rails 2.x. It defined Array.sum method which is already defined in rails 3 under Enumerable.sum. The problem with the redefine is that Array.sum no longer works with arrays of strings which is what rails was trying to do with ActionView::Template::Error.source_extract method - when trying to extract the source of the error in template, it uses the Enumerable.sum method which is redefined in a wrong way. Thus, another exception happened TypeError: cannot convert String into Fixnum and the original exception was not logged, neither was the new one. I had to do a backtrace and go through many of the internal calls to see where is the issue.
So, for everyone not seeing the actual exceptions thrown in your ActionView templates, check if you have not redefined a rails method that is used internally in rails.

Resources