Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display exception messages using simple_format #40567

Merged
merged 1 commit into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<% 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 exception. If it does there are no corrections to show.
Expand All @@ -14,5 +16,7 @@
</ul>
<% end %>
<% else %>
<h2><%= h exception.message %></h2>
<div class="exception-message">
<%= simple_format h(exception.message), { class: "message" }, wrapper_tag: "div" %>
</div>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@
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 {
border: 1px solid #D0D0D0;
border-radius: 4px;
Expand Down
14 changes: 10 additions & 4 deletions actionpack/test/dispatch/debug_exceptions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -629,15 +629,17 @@ def call(env)

# Assert correct error
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_select "div.source:not(.hidden)" do
assert_select "pre .line.active", /method_that_raises/
end

# 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/
end

Expand Down Expand Up @@ -680,7 +682,9 @@ def call(env)

# Assert correct error
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_select "div.source:not(.hidden)" do
Expand Down Expand Up @@ -749,7 +753,9 @@ def call(env)
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 "#container h2", /nil annoted_source_code/
assert_select "#container div.exception-message" do
assert_select "div", /nil annoted_source_code/
end
end

test "debug exceptions app shows diagnostics for template errors that contain UTF-8 characters" do
Expand Down
7 changes: 4 additions & 3 deletions railties/test/application/mailer_previews_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module ApplicationTests
class MailerPreviewsTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
include Rack::Test::Methods
include ERB::Util

def setup
build_app
Expand Down Expand Up @@ -299,7 +300,7 @@ def foo
app("development")
get "/rails/mailers/notifier"
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately without this change, this test was failing on rails 2.7. If I updated to "Mailer preview 'notifier' not found" this test failed on rails 2.6.5

Example of test failure without this change on 2.7

Failure:
ApplicationTests::MailerPreviewsTest#test_mailer_preview_not_found [test/application/mailer_previews_test.rb:303]:
Expected /Mailer\ preview\ &\#39;notifier&\#39;\ not\ found/ to match # encoding: ASCII-8BIT

Html from mailer (snipped)

 <h1>Unknown action</h1>\n</header>\n<div id=\"container\">\n    <div class=\"exception-message\">\n    <div class=\"message\">Mailer preview 'notifier' not found</div>\n  </div>\n\n</div>\n\n\n</body>\n</html>\n".

end

test "mailer preview email not found" do
Expand Down Expand Up @@ -329,7 +330,7 @@ def foo

get "/rails/mailers/notifier/bar"
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

test "mailer preview NullMail" do
Expand Down Expand Up @@ -385,7 +386,7 @@ def foo

get "/rails/mailers/notifier/foo?part=text%2Fhtml"
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

test "message header uses full display names" do
Expand Down