Confused by ruby / rails exception stack - ruby-on-rails

I am fighting this exception:
ActionView::TemplateError (incompatible character encodings: UTF-8 and ASCII-8BIT) on line #5 of app/views/retain/qs/_qs_row.html.erb:
That is from a template and the excpetion starts with:
app/helpers/retain/qs_helper.rb:111:in `render_row'
app/views/retain/qs/_qs_row.html.erb:5
...
That line of code is:
cache(tag) do
...
end
And at that point I'm confused. According to the stack, we are not inside cache nor are we inside the block that cache yields to -- let we are somehow, somewhere, operating on two strings (probably concatenating them. How do I find out where that operation is happening and the parameters, etc being used?
The frustrating thing also is that I can not recreate this myself. I have to put this into my staging environment and let one of my users in Korea (two of them actually) bump into it.
Anyone have any debugging suggestions? Would it help if I put a rescue in, catch the exception, and print the stack out myself? Would it include more lines?

add to your qs_helper.rb helper file this line
# encoding: utf-8
Related topic
Add "# coding: utf-8" to all files

I added my own rescue and then logged the exception's backtrace. It contained several stack levels below the one that was printed out normally. The exception is coming from within memcache in my case.
I am not sure why the Rails exception handler is not displaying the first 6 or so levels of the stack.

Related

Ruby unable to catch Selenium::WebDriver::Error::StaleElementReferenceError

I have created a method that is called for each web element accessed by the scripts, as to avoid the "StaleElementReferenceError" thrown by selenium. This is how the code looks:
def reach_element(page,element)
begin
element.wait_until_present
rescue Selenium::WebDriver::Error::StaleElementReferenceError
puts '* retrying to reach element'
page.refresh
retry
end
end
It appears that the StaleElementReferenceError is ignored and the tests keep failing with this error.
Am I doing something wrong?
CORRECTIONS:
This error shouldn't appear at all for it to be rescued by ruby.
The main cause was the old version of watir-webdriver gem. If you still encounter this error, a simple gem update should do the trick.
We mostly got rid of stale element issues for when you take an action on an element in watir-webdriver last year. This is the code: https://github.com/watir/watir-webdriver/blob/master/lib/watir-webdriver/elements/element.rb#L597
When an action is taken on the element, but it is stale, it will re-look it up with the selector provided. It will fail for not existing if it isn't there.
Are you seeing your element go stale between when you locate it, but before it becomes visible? That's an interesting use case that I have plans to fix. If that is your issue, refreshing the page will force the element to go stale, so that will just repeat your issue. Remove the refresh, and it should keep relocating the stale element until it is present.
If that isn't the issue or that doesn't work, provide a stack trace of what you are seeing.

Possible to mute irrelevant error messages?

If I erroneously would call a private method in the console, I'm getting 25 lines of error messages like the following:
from /Users/Omonia/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
The first line normally gives meaningful information but the rest could easily be muted.
Is there any way to clean this up?
If you're using the standard IRB REPL as rails console, adding the following line :
IRB.conf[:BACK_TRACE_LIMIT] = 1
to your ~/.irbrc file truncates the output to the desired limit.
However I would advise against doing so, because error backtraces can be immensely helpful in complex debugging cases.

How to extend the String class in a command line app?

I primarily work in Rails and I'm using a command line data conversion gem, "Mongify" and I am stumped about how to extend core classes in a Ruby cli app.
I want to extend the String class with an .is_date? method to check whether a string can be converted to a Date. I've got it working in the Rails Console,
I added a string.rb file to lib/ext with the following;
class String
def is_date?
begin
return true if Date.parse(self)
rescue
#do nothing
end
return false
end
end
Then in a Rails console I do a require 'ext/string' and it will work.
But I can't figure out how to get it to work in the Mongify cli app. I copied string.rb into the lib folder of the gem and I've tried adding require 'string' to a number of different files in the gem, but I keep getting undefined method errors.
Can someone point me in the right direction?
How about you require it from lib/mongify.rb like so:
require 'string/extensions.rb'
And then put your code in lib/string/extensions.rb
Let us know the exact undefined method errors you're getting in case this isn't the solution.
To help you with the debugging exercise that would give you the answer you need. Start by putting a breakpoint right before the place of the function call.
In the debugger, load the required document and then step past your breakpoint to the next one after the call has occurred.
Once you have this working, then start earlier in the stack trace – in a file that loaded before that one. Keep moving backwards until you get to a sufficiently early part in the load process of the gem, and make that be the place you load your code.

Trouble on retrieving a constant's value stated in an initializer

I am using Ruby on Rails 3.1 and I would like to understand why a constant's value stated in an initializer file sometimes is not retrieved as expected.
That is, in my ROOT_RAILS/config/initializers/initializer_name.rb file I have "simply" the following code:
CONSTANT_NAME = [
'value_one',
'value_two',
'value_three'
]
In a my view_file_name.js.erb file I have "simply" the following code:
<% logger.debug "#{CONSTANT_NAME.inspect}" %>
When I perform a HTTP request so to "trigger"/"run" the view_file_name.js.erb source code and then I go to check the log file, sometimes the CONSTANT_NAME is outputted, sometimes it isn't (in the latter case it outputs a [] value). I tried to restart the server many times but, after a while, the constant outputted to the log became [].
What is the problem? How can I solve that?
P.S.: I noted that the issue occurs mostly when I reload the page. This is a strange behavior and maybe there is something really subtle in my poor code that I can not fix. I think that the issue is related to the "process" to retrieve the CONSTANT_NAME value from the initializer file...

Rails production environment breaks with cycle

Have a rails app (3.0.9) using HAML, local development server runs fine. But when I run rails s -e production, my page gives this error:
NoMethodError: undefined method `+#' for #<String:0x00000006331098>
The error says it is on this line (from the view, written in HAML):
%tr{:class=> cycle("even","odd")}
I'm not finding anything about why this is happening. Please help.
Does the cycle method do any sort of string concatenation?
I encountered this error recently during a code review.
The code was something like this:
anObject.instance_method +string_var
The instance_method was returning a string which was to be appended with the string value present in variable string_var.
Changing the code to this worked
anObject.instance_method + string_var # Note the space after the +
Without space the unary + method is invoked on the string_var, but no unary + method is defined on the String class. Hence the exception.
Note that the unary + method is defined as def +#, hence the exception message says "Method +# not found".
This gist makes it clear : https://gist.github.com/1145457
Anyways, in your case, the method cycle (do not know whether it is defined by you or is part of a gem) is probably doing some string concatenation without proper spacing OR the exception backtrace is not pointing to the right line of code.
Hope this helps.

Resources