Hello, When I call an action with some invalid XML or JSON data a parse exception gets raised from within Rails/Ruby (REXML or ActiveSupport::JSON). The problem that I''ve got is how to handle these exceptions. In my application_controller.rb I have the following for debugging purposes: def rescue_action_in_public(exception) respond_to do |request| request.all { render :text => "Uh oh" } end end and the same for #rescue_action_locally. It seems that if the Content- Type header of the request is set to application/xml or application/ json then Rails ignores my exception rescuing code and goes back a FAILSAFE 500 error. Anyone know why this is or what I''m doing wrong? Thanks
Hi Alistair, On Fri, 2009-08-14 at 08:13 -0700, subv3 wrote:> Hello, > > When I call an action with some invalid XML or JSON data a parse > exception gets raised from within Rails/Ruby (REXML or > ActiveSupport::JSON). The problem that I''ve got is how to handle these > exceptions. In my application_controller.rb I have the following for > debugging purposes:> def rescue_action_in_public(exception) > respond_to do |request| > request.all { render :text => "Uh oh" } > end > end > > and the same for #rescue_action_locally. It seems that if the Content- > Type header of the request is set to application/xml or application/ > json then Rails ignores my exception rescuing code and goes back a > FAILSAFE 500 error. Anyone know why this is or what I''m doing wrong?No conclusive answer, but a couple of suggestions that might help move you along... First, I''d get this working in a specific controller before moving it to application.rb. Second, ''request'' refers to the request object and might get you into trouble here. Lastly, from looking at the respond_to source, it looks like the ''catch all'' type specifier is ''any'', not ''all''. But I could be reading it wrong. You might want to take a look. HTH, Bill
Hi Bill, The problem isn''t in the respond_to block. Even a straight call to render some text without using respond_to still falls back to the FAILSAFE 500 error. Cheers On Aug 14, 5:22 pm, bill walton <bwalton...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Alistair, > > > > > > On Fri, 2009-08-14 at 08:13 -0700, subv3 wrote: > > Hello, > > > When I call an action with some invalid XML or JSON data a parse > > exception gets raised from within Rails/Ruby (REXML or > > ActiveSupport::JSON). The problem that I''ve got is how to handle these > > exceptions. In my application_controller.rb I have the following for > > debugging purposes: > > def rescue_action_in_public(exception) > > respond_to do |request| > > request.all { render :text => "Uh oh" } > > end > > end > > > and the same for #rescue_action_locally. It seems that if the Content- > > Type header of the request is set to application/xml or application/ > > json then Rails ignores my exception rescuing code and goes back a > > FAILSAFE 500 error. Anyone know why this is or what I''m doing wrong? > > No conclusive answer, but a couple of suggestions that might help move > you along... First, I''d get this working in a specific controller > before moving it to application.rb. Second, ''request'' refers to the > request object and might get you into trouble here. Lastly, from > looking at the respond_to source, it looks like the ''catch all'' type > specifier is ''any'', not ''all''. But I could be reading it wrong. You > might want to take a look. > > HTH, > Bill
On Aug 14, 11:13 am, subv3 <alistairh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > When I call an action with some invalid XML or JSON data a parse > exception gets raised from within Rails/Ruby (REXML or > ActiveSupport::JSON). The problem that I''ve got is how to handle these > exceptions. In my application_controller.rb I have the following for > debugging purposes: > > def rescue_action_in_public(exception) > respond_to do |request| > request.all { render :text => "Uh oh" } > end > end > > and the same for #rescue_action_locally. It seems that if the Content- > Type header of the request is set to application/xml or application/ > json then Rails ignores my exception rescuing code and goes back a > FAILSAFE 500 error. Anyone know why this is or what I''m doing wrong? > > ThanksWhat is the actual exception being raised? I''m having trouble reproducing this: class TotalController < ApplicationController def blow_up respond_to do |wants| wants.json { render :json => make_ish } wants.html { render :text => "hello" } end end def rescue_action_locally(e) render :text => "resuced" end private def make_ish raise "it up" {:yo => :max!} end end map.connect "die.:format", :controller => "total", :action => "blow_up" xeno@amrita:~$ wget amrita/pixie/die.html --2009-08-14 16:34:26-- amrita/pixie/die.html Resolving amrita... 127.0.1.1 Connecting to amrita|127.0.1.1|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 6 [text/html] Saving to: `die.html'' 2009-08-14 16:34:26 (925 KB/s) - `die.html'' saved [6/6] xeno@amrita:~$ cat die.html hello xeno@amrita:~$ wget amrita/pixie/die.json --2009-08-14 16:34:34-- amrita/pixie/die.json Resolving amrita... 127.0.1.1 Connecting to amrita|127.0.1.1|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 8 [application/json] Saving to: `die.json'' 2009-08-14 16:34:34 (913 KB/s) - `die.json'' saved [8/8] xeno@amrita:~$ cat die.json resuced xeno@amrita:~$ As you can see, rescue_action_locally is being called, and the content- type of the response is set to application/json. Or does it look there theres something my setup is missing that yours has?
> On Aug 14, 11:13 am, subv3 <alistairh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: [...]bleh, need to read the thread title. Either way, changed make_ish to raise REXML::ParseException exception is still being rescued as expected :\ what''s missing?
I think the exception I''m dealing with happens before it has a change to get caught by the rescue_action* methods. Maybe my backtrace will give some insight. gist.github.com/168372 On Aug 14, 9:48 pm, pharrington <xenogene...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Aug 14, 11:13 am, subv3 <alistairh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: [...] > > bleh, need to read the thread title. Either way, changed make_ish to > > raise REXML::ParseException > > exception is still being rescued as expected :\ what''s missing?
Typo.. change = chance On Aug 15, 4:11 pm, Alistair Holt <alistairh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I think the exception I''m dealing with happens before it has a change > to get caught by the rescue_action* methods. Maybe my backtrace will > give some insight. > > gist.github.com/168372 > > On Aug 14, 9:48 pm, pharrington <xenogene...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > On Aug 14, 11:13 am, subv3 <alistairh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: [...] > > > bleh, need to read the thread title. Either way, changed make_ish to > > > raise REXML::ParseException > > > exception is still being rescued as expected :\ what''s missing?
For anyone else browsing this discussion. groups.google.co.uk/group/northwest-tech-startup/browse_thread/thread/d97334ed0fcee2d8 is tackling the same problem but came up with no solution.
I''ve decided to bypass ActionPack and use Rails metal instead (I was going to use it for this eventually anyway its an API which needs to be fast). Now I can parse the parameters myself in Rack and rescue incoming requests that send invalid JSON/XML data nicely.
in case anybody gets here waiting for an answer try the following plugin to deal with XML/JSON parse errors : github.com/kares/request_exception_handler
Maybe Matching Threads
- REXML::ParseException - but the feed IS valid
- REXML::ParseException: Missing end tag for 'META' (got "HEAD
- Anyone know why __END__ and DATA don't work in Rails?
- rendering error page for "Unauthorized" from before_filter
- Save the results of data analysis in R