Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

Exceptions in views hard to catch

#2034

I had a ticket open in the old Rails bug system about this along with a patch. That patch no longer works.

I have a couple of questions / concerns.

First, if a template encounters an exception during "compile" time, the original exception is caught and wrapped in a TemplateError exception. Thats fine but if an exception occurs during "run" time, the exception is also caught and also wrapped in a TemplateError. It would be nice if those two wrappers were different.

Second, I set a catch for a particular exception but if that exception happens while in a view, then the view code catches it and wraps it. When it gets to action controller, now my exception handler is not used because it looks like a TemplateError exception.

Third, the second item above would not be too bad if there was a defined way to catch a TemplateError exception (or any exception), examine it, and if I don't want to really catch it, return "false" or something like that from my exception handler and have it be caught by the default handlers. (I guess calling rescue_action_without_handler would work but is that "stable"?)

I would not mind working on a patch for all this but it gets frustrating submitting patches for the same issue over and over again.

Reported by pedz · February 21st, 2009 @ 02:54 AM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: none

Activity

  1. pedz
  2. Michael Koziarski
    Michael Koziarski
    • Milestone cleared.
    • Assigned user set to Michael Koziarski

    I was bitten by this myself today, marking for 3.0

    I'd prefer to investigate changing AV to not swallow the exceptions before making this change in rescue_from

    July 27th, 2009 @ 04:27 AM

  3. Michael Koziarski
  4. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • State changed from new to invalid

    I cannot reproduce. If anyone can, please provide better repro information.

    June 8th, 2010 @ 02:09 AM

  5. Evgeniy Dolzhenko
    Evgeniy Dolzhenko

    Hopefully this sufficiently illustrates the problem we're having

    application_controller.rb

    class ApplicationController < ActionController::Base
      protect_from_forgery
      layout 'application'
      
      rescue_from ActionView::TemplateError, :with => :rescue_template_error
    
      def rescue_template_error(template_error)
        # any exception raised from views will be wrapped in TemplateError
        # so that you have to examine `template_error.original_exception` and
        # then use `rescue_action_without_handler` if exception should propagate 
        render :text => "rescued TemplateError"
      end
    
      rescue_from ArgumentError, :with => :rescue_argument_error
    
      def rescue_argument_error(argument_error)
        # this will get called only when raised from controller level
        render :text => "rescued ArgumentError"
      end
    end
    

    posts/index.html.erb

    <%# will get caught by `rescue_from ActionView::TemplateError` clause  %>
    <% raise ArgumentError, "test ArgumentError" %>
    

    posts_controller.rb

    class PostsController < ApplicationController
      def index
        # will get caught by `rescue_from ArgumentError, :with => :rescue_argument_error` clause
        raise ArgumentError, "test ArgumentError"
      end
    end
    

    June 8th, 2010 @ 04:49 AM

  6. Neeraj Singh
    Neeraj Singh
    • Tag changed from action_view, exception, templateerror to 3.x, action_view, exception, patch, templateerror

    Attached is a patch with test.

    June 8th, 2010 @ 10:22 AM

  7. Neeraj Singh
    Neeraj Singh

    José Valim provided feedback on the patch. Will be providing a better patch and test soon.

    June 8th, 2010 @ 02:40 PM

  8. Neeraj Singh
    Neeraj Singh

    Attached is modified patch with test.

    June 8th, 2010 @ 05:09 PM

  9. José Valim
    José Valim
    • State changed from invalid to open
    • Assigned user changed from Michael Koziarski to José Valim
    • Importance changed from to

    July 19th, 2010 @ 01:00 PM

  10. Repository
    Repository
    • State changed from open to resolved

    (from [33c5689e2d04aa08759903bc5d1e4de3bf6c35dd]) Exceptions from views should be rescued based on the original exception. If a handler for original exception is missing then apply ActiveView::TemplateError

    [#2034 Exceptions in views hard to catch state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/33c5689e2d04aa08759903bc5d1e4d...

    July 19th, 2010 @ 01:51 PM

  11. Jon Leighton
    Jon Leighton

    Here is a workaround for those who are using Rails 2.3. Place in your ApplicationController:

    rescue_from ActionView::TemplateError do |exception|
      rescue_action(exception.original_exception)
    end
    

    August 4th, 2010 @ 10:58 AM

  12. Jeremy Kemper