bajandude-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-Jan-15 10:43 UTC
Error catching in Prototype?
I have studiously avoided JavaScript for many years now, having had to deal with it back in the Netscape 4 days and subsequently going insane. I''m now trying to dip my toes back into the JS water, and am working with Prototype to provide server-side validation using the Ajax functionality of Prototype. In general, this is working just fine, but there''s one part of the experience that''s driving me nuts. Assume this sample code (-[ ]- come from the fact this is in a Smarty template): function processRequest() { var url = ''-[$serverpath]-''; var parameters = Form.serialize($(''dataform'')); var myAjax = new Ajax.Request( url, { method: ''-[$ajaxMethod]-'', parameters: parameters, onSuccess: function(request, json) { // Clear all error spans clearFormErrors(); // Clear the result span clearStatusNotification(); // Set the result text. $(''result'').update(json.resultText); $(''result'').show(); // Check the JSON data if (json.success == false) { // Server said life is bad if (json.errors) { new Insertion.Top(''errors'', ''<span><ul>''); json.errors.each(function(m) { insertion = ''<li id="error_''+m+''">''+json.message[m]+''</li>''; new Insertion.Bottom(''errors'', insertion); $(''error_''+m).addClassName(''error''); }); new Insertion.Bottom(''errors'', ''</ul></span>''); $(''errors'').addClassName(''errors''); $(''errors'').show(); } $(''result'').addClassName(''error''); new Effect.Highlight($(''result''), {startcolor:''#ff0000'', endcolor:''#ff3333'', restorecolor:''#ff3333'', duration: 3.0}); .... If there is a single mistake on any of the lines in the onSuccess anonymous function, the JS appears to stop processing, and not a single error gets logged in Firefox''s console (nor in Firebug). This has led to several hours of me looking at the code and going ''But everything looks right!'', only to realise (after copious use of alert()) that I called a custom function by the wrong name. What methodology/function/voodoo can I use to get JS errors to occur when the anonymous function goes belly up? I''m using an RC of the 1.5 version of Prototype to get the automatic JSON header parsing (controlled environment, not a public site). --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On 15/01/07, bajandude-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org <bajandude-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > > If there is a single mistake on any of the lines in the onSuccess > anonymous function, the JS appears to stop processing, and not a single > > What methodology/function/voodoo can I use to get JS errors to occur > when the anonymous function goes belly up? I''m using an RC of the 1.5 > version of Prototype to get the automatic JSON header parsing > (controlled environment, not a public site). > >Well, a bit of digging later (darn searches for the wrong item, Venkman debugger++), I''ve found that I can declare onException: function(request,transport) and get access to transport.message for telling me what went wrong. Hopefully exceptions won''t be common in the code, but I''ll leave the handler in anyway, and make it do something nice. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Monday 15 January 2007 13:49, Duncan Hill wrote:> On 15/01/07, bajandude-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org <bajandude-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > If there is a single mistake on any of the lines in the onSuccess > > anonymous function, the JS appears to stop processing, and not a single > > > > Well, a bit of digging later (darn searches for the wrong item, Venkman > debugger++), I''ve found that I can declare onException: > function(request,transport) and get access to transport.message for telling > me what went wrong. Hopefully exceptions won''t be common in the code, but > I''ll leave the handler in anyway, and make it do something nice. >I''ve found this to be problematic at times too - if you''re chaining together several functions in response to an async callback, then large portions of your code will fall under the exception trap. Adding an onException handler that simply re-throws the exception doesn''t give you full access to the stack trace. Usuallly, I just hack prototype.js and temporarily remove the try/catch that invokes the onException callback (in 1.5.0_rc1, shipping with the latest scriptaculous, it''s around lines 750-790). Not very elegant, but the debuggers like it. Make sure you only comment out the try and catch, not all the stuff inbetween!! HTH Dave -- ---------------------- Author Ajax in Action http://manning.com/crane Ajax in Practice http://manning.com/crane2 Prototype & Scriptaculous Quickly http://manning.com/crane3 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On 15/01/07, Dave Crane <dave-qrf20pp95eSLQvtTh0HkdajZmZ73YKuj@public.gmane.org> wrote:> > > > Well, a bit of digging later (darn searches for the wrong item, Venkman > > debugger++), I''ve found that I can declare onException: > > function(request,transport) and get access to transport.message for > telling > > me what went wrong. Hopefully exceptions won''t be common in the code, > but > > I''ll leave the handler in anyway, and make it do something nice. > > > I''ve found this to be problematic at times too - if you''re chaining > together > several functions in response to an async callback, then large portions of > your code will fall under the exception trap. Adding an onException > handler > that simply re-throws the exception doesn''t give you full access to the > stack > trace. Usuallly, I just hack prototype.js and temporarily remove the > try/catch that invokes the onException callback (in 1.5.0_rc1, shipping > withCheers Dave, I''ll bear that in mind - I keep everything in subversion, so if I screw up it isn''t too hard to fix :> I''m not trying to do anything that requires chaining yet - just simple (hah!) tweaks to make the interface a bit more pleasant than ''type, submit, wait for reload, bugger, messed up''. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---