Monday, November 18, 2013

[Rails] Re: Hook into Exception Chain

Thanks a lot! That's useful

On Saturday, 16 November 2013 22:36:35 UTC+1, Frederick Cheung wrote:


On Friday, November 15, 2013 10:26:10 AM UTC, sol wrote:
I've used the above approach now

However, this only works for controller actions.
I've got some cronjobs in the project (lib folder) as well as a sinatra app that is mounted within the rails app.

Is there any way I could catch the exceptions in these parts as well?

For a cronjob type thing it's up to you to wrap your script with something that will log the error. My cronjobs normally end up looking like

TaskWrapper('some job') do
  #work here
end

and elsewhere

def TaskWrapper
  begin
    yield
  rescue Exception => ex
    #do something with the exception
    raise
  end
end

I use rescue Exception because for once I think it's appropriate that things like SyntaxError are caught so that I can be notified about them

For the sinatra (or even the rails case) you could write a rack middleware that would look something like

class ErrorNotifier
  def initialize(app)
    @app = app
  end

  def call(env)
    begin
      @app.call(env)
    rescue Exception => ex
      #do something with the exception
      raise
   end
  end
end

 
You could also render an error message if you want to override that.

You might also be interested in the exception handling stuff described at http://blog.plataformatec.com.br/2012/01/my-five-favorite-hidden-features-in-rails-3-2/

Fred

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/68616ea1-d09e-4b2b-a8b2-8e844fbae616%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate