This project is archived and is in readonly mode.
Exceptions in views hard to catch
Reported by pedz | February 21st, 2009 @ 02:54 AM | in 3.0.2
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.
Comments and changes to this ticket
-
Michael Koziarski July 27th, 2009 @ 04:27 AM
- 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
-
Michael Koziarski June 1st, 2010 @ 10:25 PM
- Milestone cleared.
-
Yehuda Katz (wycats) June 8th, 2010 @ 02:09 AM
- State changed from new to invalid
I cannot reproduce. If anyone can, please provide better repro information.
-
Evgeniy Dolzhenko June 8th, 2010 @ 04:49 AM
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
-
Neeraj Singh June 8th, 2010 @ 10:22 AM
- Tag changed from action_view, exception, templateerror to 3.x, action_view, exception, patch, templateerror
Attached is a patch with test.
-
Neeraj Singh June 8th, 2010 @ 02:40 PM
José Valim provided feedback on the patch. Will be providing a better patch and test soon.
-
José Valim July 19th, 2010 @ 01:00 PM
- State changed from invalid to open
- Assigned user changed from Michael Koziarski to José Valim
- Importance changed from to
-
Repository July 19th, 2010 @ 01:51 PM
- 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 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/33c5689e2d04aa08759903bc5d1e4d... -
Jon Leighton August 4th, 2010 @ 10:58 AM
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
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 2034 Exceptions in views hard to catch [#2034 state:resolved]