1

I am attempting to rescue RuntimeErrors from within my controller using rescue_from, log it as a HealthNotice, and then redirect the user with a flash message, but I'm finding that in certain circumstances, the rescue_from method is being run twice (I end up with 2 duplicate HealthNotices). If the exception is rasied from a function called in a method (such as the test method below), it only creates one, but if I raise the exception directly within the method (such as test2), the rescue function is running twice (although strangely I don't get a double render error -- ??)

class Portal::BluerimController < PortalController
    rescue_from RuntimeError, with: :error_handler
    def error_handler(e)
      hn_long_message = "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}\n\nPARAMS:\n#{params}"
      HealthNotice.create(name: "API Exception Handled", severity: "WARNING", short_message: e.message, long_message: hn_long_message)
      flash[:notice] = e.message
      redirect_to :action => :index and return
    end

    def test
      Sotal.new.test  # a method that raises a RuntimeError -- only 1 HealthNotice
    end

    def test2
      raise "an error"  # results in duplicate HealthNotice
    end
end

Any suggestions on what I'm doing wrong? Thanks!

Lannar
  • 143
  • 1
  • 6
  • Doesn't make sense, can you add the logs showing from the request for `test2`, and also add your `config/routes.rb` and both `PortalController` and `ApplicationController` – smathy Feb 19 '16 at 17:14
  • not very easily -- PortalController is ~3500 lines long. Can you advise what sort of thing I should try to look for in there? When I watch the logs, I'm basically seeing two GET requests back to back for the same action (and subsequent HealthNotice create, and redirection) – Lannar Feb 19 '16 at 17:46
  • Let's start with the logs and routes then. – smathy Feb 19 '16 at 17:53
  • For whatever reason, this is only happening when I use chrome. If I hit the test action with IE, it redirects OK and only creates 1 HealthNotice. also, SOME of the time, chrome works as expected as well. It definitely seems to be an issue of multiple requests hitting the test action in sequence. I think I'm running up against the issue mentioned here: http://stackoverflow.com/questions/4460661/what-to-do-with-chrome-sending-extra-requests – Lannar Feb 19 '16 at 18:23
  • I can confirm that if I turn off "Prefetch resources to load pages more quickly.” under chrome settings -> advanced -> privacy, the issue goes away. I'm kind of surprised I've never run into this issue before, but I also don't know of a good way to handle it in my scenario.. – Lannar Feb 19 '16 at 18:30
  • Well, if a browser (or anything) is requesting the page twice then your app is correctly logging two requests. – smathy Feb 19 '16 at 22:17

0 Answers0