JavaScript errors to an alert box injure my soul. Is there a way to send it to console.log()
instead of alert()
?

- 30,962
- 25
- 85
- 135

- 13,361
- 9
- 46
- 63
-
for rjs, try out http://maintainable.com/software/firebug\_rjs\_errors (somehow the link does not work when embedded) – tliff Aug 18 '09 at 22:34
4 Answers
You could override window.alert:
var oldAlert = window.alert; // reference to the original window.alert
window.alert = function(message) {
if (window.console && console.log) {
console.log(message);
} else {
oldAlert(message); // if console.log doesn't exist call window alert
}
}

- 807,428
- 183
- 922
- 838
-
2That would work but that's quite brutal imho. But still good solution so +1 – marcgg Aug 18 '09 at 22:45
-
Solution for now! But I really want to explore that plugin from below--just in case I need to use alert in the app (which I never do, I consider popups in nearly any form profanity) – Ryan Florence Aug 18 '09 at 23:12
-
-
In JS confirm and alert are different, though they are both agitating. – Ryan Florence Aug 19 '09 at 02:58
Yes, console.log("error message goes here")

- 106,965
- 23
- 235
- 261
-
-
Yes, Rails does a lot of stuff on its own, I suppose I should add the tag rjs – Ryan Florence Aug 18 '09 at 23:07
It depends on the Javascript error. Since JavaScript is interpreted by the client, you would need to display the error messages client-side. The only real way to log errors in the server log file is if the error is happening on the server side before the Javascript is interpreted by the client.

- 150
- 5
-
You're absolutely right, but console.log is a javascript function for browser debugging tools (webkit's inspector, firebug, and IE8's new thing) that I'm trying to use instead of Rails default alert(). – Ryan Florence Aug 18 '09 at 23:09
You could try turning off RJS debugging of in your environment.rb file:
config.action_view.debug_rjs = false
This should turn off the alerts altogether. Another option would be overriding ActionView::Helpers::GeneratorMethods#to_s
:
module ActionView
module Helpers
module GeneratorMethods
def to_s #:nodoc:
returning javascript = @lines * $/ do
if ActionView::Base.debug_rjs
source = javascript.dup
javascript.replace "try {\n#{source}\n} catch (e) "
javascript << "{ console.log('RJS error:\\n\\n' + e.toString()); console.log('#{source.gsub('\\','\0\0').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }}'); throw e }"
end
end
end
end
end
end
I admit I'm out of my depth as far as overriding the method goes, and I don't know if it's a recommended practice. I haven't needed to do anything like this in my projects.
My personal preference would be to skip RJS and go with unobtrusive jQuery.

- 31,495
- 6
- 74
- 83
-
Not sure how prototype is more obtrusive ... but anyway, I'm going to give prototype/scriptaculous a shot. Most the stuff I do is xhr heavy, so I'd like to "keep it in the family" instead of messing around with JS separately. I'm a mootools man myself and can already see myself wanting to use it. – Ryan Florence Aug 19 '09 at 03:01
-
I'm sure you can go unobtrusive with prototype but that would mean giving up using a lot of the helpers AFAIK. I like writing and controlling my JS so it works for me. – Andy Gaskell Aug 19 '09 at 04:00