Skip to content

Proposal - Pretty print error structs in debugger page #1126

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

Merged
merged 4 commits into from
Jan 7, 2023

Conversation

leandrocp
Copy link
Contributor

I couldn't find such proposal in past issues/PRs so this one is a simple PoC to gather feedback if that's a good idea or not.

In short, currently the debugger page looks like this:

Screenshot 2023-01-06 at 1 53 48 PM

And the idea is to improve UX by displaying this:

Screenshot 2023-01-06 at 1 53 20 PM

Notes:

  • It may get too long so a toggle link to show/hide details could help
  • Pattern matching the error to pretty inspect it instead of calling Exception.message/1 seems okay to deal with some exceptions but it could get out of sync with Elixir stdlib if wording changes.

@josevalim
Copy link
Member

It feels like we should just format the whole error message as <code>, because they are written with assumption they will be printed monospaced?

@leandrocp
Copy link
Contributor Author

It feels like we should just format the whole error message as <code>, because they are written with assumption they will be printed monospaced?

Do you mean the error message from the original code? 🤔 The problem here is that Exception.message/1 does return a string with no line breaks, that's why I had to reformat as inspect(term, pretty: true, limit: :infinity)

@josevalim
Copy link
Member

Let’s change Elixir to use pretty for this message and also change Plug to use code style for the error message :)

@leandrocp
Copy link
Contributor Author

Gotcha. I can send a PR to Elixir and then update this one.

to pretty print KeyError message, and improve layout styling.
<h5 class="struct">
<%= h @title %>
<small>at <%= h method(@conn) %></small>
<small class="path"><%= h @conn.request_path %></small>
</h5>
<h1 class="title"><%= h headline %></h1>
<%= for detail <- details do %>
<code class="headline"><pre><%= h @headline %></pre></code>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using <pre> to represent multi lines as noted here https://developer.mozilla.org/en-US/docs/Web/HTML/Element/code#notes

@leandrocp
Copy link
Contributor Author

So that's how a simple message would like:
Screenshot 2023-01-06 at 4 10 54 PM

And how a long KeyError would like:
Screenshot 2023-01-06 at 4 11 57 PM
Screenshot 2023-01-06 at 4 12 10 PM

@leandrocp leandrocp marked this pull request as ready for review January 6, 2023 21:17
@josevalim
Copy link
Member

I think we can keep the style closer to what you had: grey and smaller font. :)

@leandrocp
Copy link
Contributor Author

I think we can keep the style closer to what you had: grey and smaller font. :)

For all error messages like the MatchError example above? Because they all share the same style.

@leandrocp
Copy link
Contributor Author

That's how it would like, grey and smaller font:

A regular exception:
Screenshot 2023-01-06 at 4 56 41 PM

And KeyError with pretty: true:
Screenshot 2023-01-06 at 4 57 09 PM

@josevalim
Copy link
Member

Perfect. Let's do said changes and for all error messages. :)

@@ -146,25 +145,6 @@
font-weight: 400;
}

.exception-info > .title {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed because the default 1em font looks good across all sizes.

@@ -187,6 +187,7 @@ defmodule Plug.Debugger do
session = maybe_fetch_session(conn)
params = maybe_fetch_query_params(conn)
{title, message} = info(kind, reason)
[headline | details] = String.split(message, "\n\n")
Copy link
Member

Choose a reason for hiding this comment

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

I think we no longer need to split between headline and details, no?

Copy link
Member

Choose a reason for hiding this comment

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

Rather just always format everything as code. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so, done 👍🏻

@josevalim josevalim merged commit 1544509 into elixir-plug:main Jan 7, 2023
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@leandrocp leandrocp deleted the lcp-pretty-print-struct branch January 7, 2023 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants