提交 297a579d 编写于 作者: V Vinicius Stock

Create MissingExactTemplate exception with separate template

上级 b6ee4e4b
......@@ -50,4 +50,7 @@ class UnknownHttpMethod < ActionControllerError #:nodoc:
class UnknownFormat < ActionControllerError #:nodoc:
end
class MissingExactTemplate < UnknownFormat #:nodoc:
end
end
......@@ -41,18 +41,8 @@ def default_render(*args)
raise ActionController::UnknownFormat, message
elsif interactive_browser_request?
message = "#{self.class.name}\##{action_name} is missing a template " \
"for this request format and variant.\n\n" \
"request.formats: #{request.formats.map(&:to_s).inspect}\n" \
"request.variant: #{request.variant.inspect}\n\n" \
"NOTE! For XHR/Ajax or API requests, this action would normally " \
"respond with 204 No Content: an empty white screen. Since you're " \
"loading it in a web browser, we assume that you expected to " \
"actually render a template, not nothing, so we're showing an " \
"error to be extra-clear. If you expect 204 No Content, carry on. " \
"That's what you'll get from an XHR or API request. Give it a shot."
raise ActionController::UnknownFormat, message
message = "#{self.class.name}\##{action_name} is missing a template for request formats: #{request.formats.map(&:to_s).join(',')}"
raise ActionController::MissingExactTemplate, message
else
logger.info "No template found for #{self.class.name}\##{action_name}, rendering head :no_content" if logger
super
......
......@@ -12,6 +12,7 @@ class ExceptionWrapper
"ActionController::UnknownHttpMethod" => :method_not_allowed,
"ActionController::NotImplemented" => :not_implemented,
"ActionController::UnknownFormat" => :not_acceptable,
"ActionController::MissingExactTemplate" => :not_acceptable,
"ActionController::InvalidAuthenticityToken" => :unprocessable_entity,
"ActionController::InvalidCrossOriginRequest" => :unprocessable_entity,
"ActionDispatch::Http::Parameters::ParseError" => :bad_request,
......@@ -22,11 +23,12 @@ class ExceptionWrapper
)
cattr_accessor :rescue_templates, default: Hash.new("diagnostics").merge!(
"ActionView::MissingTemplate" => "missing_template",
"ActionController::RoutingError" => "routing_error",
"AbstractController::ActionNotFound" => "unknown_action",
"ActiveRecord::StatementInvalid" => "invalid_statement",
"ActionView::Template::Error" => "template_error"
"ActionView::MissingTemplate" => "missing_template",
"ActionController::RoutingError" => "routing_error",
"AbstractController::ActionNotFound" => "unknown_action",
"ActiveRecord::StatementInvalid" => "invalid_statement",
"ActionView::Template::Error" => "template_error",
"ActionController::MissingExactTemplate" => "missing_exact_template",
)
attr_reader :backtrace_cleaner, :exception, :line_number, :file
......
<header>
<h1>No template for interactive request</h1>
</header>
<div id="container">
<h3><%= h @exception.message %></h3>
<p class="summary">
<strong>NOTE!</strong><br>
Unless told otherwise, Rails expects an action to render a template with the same name,<br>
contained in a folder named after its controller.
If this controller is an API responding with 204 (No Content), <br>
which does not require a template,
then this error will occur when trying to access it via browser,<br>
since we expect an HTML template
to be rendered for such requests. If that's the case, carry on.
</p>
</div>
......@@ -658,13 +658,13 @@ def test_variant_with_implicit_template_rendering
end
def test_variant_without_implicit_rendering_from_browser
assert_raises(ActionController::UnknownFormat) do
assert_raises(ActionController::MissingExactTemplate) do
get :variant_without_implicit_template_rendering, params: { v: :does_not_matter }
end
end
def test_variant_variant_not_set_and_without_implicit_rendering_from_browser
assert_raises(ActionController::UnknownFormat) do
assert_raises(ActionController::MissingExactTemplate) do
get :variant_without_implicit_template_rendering
end
end
......
......@@ -650,7 +650,7 @@ class ImplicitRenderTest < ActionController::TestCase
tests ImplicitRenderTestController
def test_implicit_no_content_response_as_browser
assert_raises(ActionController::UnknownFormat) do
assert_raises(ActionController::MissingExactTemplate) do
get :empty_action
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册