Display exception messages using simple_format for a better and clearer

exception message.
This commit is contained in:
Haroon Ahmed 2020-11-07 08:48:16 +00:00
parent e71b4a5e04
commit 93f0cebd1d
4 changed files with 32 additions and 9 deletions

View File

@ -1,5 +1,7 @@
<% if exception.respond_to?(:original_message) && exception.respond_to?(:corrections) %> <% if exception.respond_to?(:original_message) && exception.respond_to?(:corrections) %>
<h2><%= h exception.original_message %></h2> <div class="exception-message">
<%= simple_format h(exception.original_message), { class: "message" }, wrapper_tag: "div" %>
</div>
<% <%
# The 'did_you_mean' gem can raise exceptions when calling #corrections on # The 'did_you_mean' gem can raise exceptions when calling #corrections on
# the exception. If it does there are no corrections to show. # the exception. If it does there are no corrections to show.
@ -14,5 +16,7 @@
</ul> </ul>
<% end %> <% end %>
<% else %> <% else %>
<h2><%= h exception.message %></h2> <div class="exception-message">
<%= simple_format h(exception.message), { class: "message" }, wrapper_tag: "div" %>
</div>
<% end %> <% end %>

View File

@ -49,6 +49,18 @@
line-height: 25px; line-height: 25px;
} }
.exception-message {
padding: 8px 0;
}
.exception-message .message{
margin-bottom: 8px;
line-height: 25px;
font-size: 1.5em;
font-weight: bold;
color: #C00;
}
.details { .details {
border: 1px solid #D0D0D0; border: 1px solid #D0D0D0;
border-radius: 4px; border-radius: 4px;

View File

@ -629,7 +629,9 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
# Assert correct error # Assert correct error
assert_response 500 assert_response 500
assert_select "h2", /error in framework/ assert_select "div.exception-message" do
assert_select "div", /error in framework/
end
# assert source view line is the call to method_that_raises # assert source view line is the call to method_that_raises
assert_select "div.source:not(.hidden)" do assert_select "div.source:not(.hidden)" do
@ -637,7 +639,7 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
end end
# assert first source view (hidden) that throws the error # assert first source view (hidden) that throws the error
assert_select "div.source:first" do assert_select "div.source" do
assert_select "pre .line.active", /raise StandardError\.new/ assert_select "pre .line.active", /raise StandardError\.new/
end end
@ -680,7 +682,9 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
# Assert correct error # Assert correct error
assert_response 500 assert_response 500
assert_select "h2", /Third error/ assert_select "div.exception-message" do
assert_select "div", /Third error/
end
# assert source view line shows the last error # assert source view line shows the last error
assert_select "div.source:not(.hidden)" do assert_select "div.source:not(.hidden)" do
@ -749,7 +753,9 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
get "/nil_annoted_source_code_error", headers: { "action_dispatch.show_exceptions" => true, "action_dispatch.logger" => logger } get "/nil_annoted_source_code_error", headers: { "action_dispatch.show_exceptions" => true, "action_dispatch.logger" => logger }
assert_select "header h1", /DebugExceptionsTest::Boomer::NilAnnotedSourceCodeError/ assert_select "header h1", /DebugExceptionsTest::Boomer::NilAnnotedSourceCodeError/
assert_select "#container h2", /nil annoted_source_code/ assert_select "#container div.exception-message" do
assert_select "div", /nil annoted_source_code/
end
end end
test "debug exceptions app shows diagnostics for template errors that contain UTF-8 characters" do test "debug exceptions app shows diagnostics for template errors that contain UTF-8 characters" do

View File

@ -8,6 +8,7 @@ module ApplicationTests
class MailerPreviewsTest < ActiveSupport::TestCase class MailerPreviewsTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation include ActiveSupport::Testing::Isolation
include Rack::Test::Methods include Rack::Test::Methods
include ERB::Util
def setup def setup
build_app build_app
@ -299,7 +300,7 @@ module ApplicationTests
app("development") app("development")
get "/rails/mailers/notifier" get "/rails/mailers/notifier"
assert_predicate last_response, :not_found? assert_predicate last_response, :not_found?
assert_match "Mailer preview &#39;notifier&#39; not found", last_response.body assert_match "Mailer preview &#39;notifier&#39; not found", h(last_response.body)
end end
test "mailer preview email not found" do test "mailer preview email not found" do
@ -329,7 +330,7 @@ module ApplicationTests
get "/rails/mailers/notifier/bar" get "/rails/mailers/notifier/bar"
assert_predicate last_response, :not_found? assert_predicate last_response, :not_found?
assert_match "Email &#39;bar&#39; not found in NotifierPreview", last_response.body assert_match "Email &#39;bar&#39; not found in NotifierPreview", h(last_response.body)
end end
test "mailer preview NullMail" do test "mailer preview NullMail" do
@ -385,7 +386,7 @@ module ApplicationTests
get "/rails/mailers/notifier/foo?part=text%2Fhtml" get "/rails/mailers/notifier/foo?part=text%2Fhtml"
assert_predicate last_response, :not_found? assert_predicate last_response, :not_found?
assert_match "Email part &#39;text/html&#39; not found in NotifierPreview#foo", last_response.body assert_match "Email part &#39;text/html&#39; not found in NotifierPreview#foo", h(last_response.body)
end end
test "message header uses full display names" do test "message header uses full display names" do