Project

General

Profile

Actions

Feature #19019

open

Nicely formatted exception messages in HTML

Added by ioquatix (Samuel Williams) over 1 year ago. Updated over 1 year ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:110039]

Description

We have made a lot of improvements to exception formatting. See https://bugs.ruby-lang.org/issues/18296 for details.

I'd like us to consider adding support for HTML formatting of messages, e.g.

exception.full_message(highlight: :html)

or something to that effect.

Another option is to convert terminal style errors to html by converting control characters. However, I'm less optimistic about parsing sequences like ^^^^^ to apply a style to the above line.

Maybe it's sufficient when highlight: true is specified to avoid ^^^^^ characters.

When I've personally implemented this in the past, I've used a formatting object, e.g.

exception.full_message(highlight: HTMLFormatter.new)

The formatter has a rich interface for printing, and uses a set of abstractions to map to the underlying output.

e.g.

formatter.puts(:exception, "NoMethodError", :reset, ": ", :message, "undefined method 'bar' for ", :object, "#<Foo...>")
formatter.puts(:code, "x = foo", :error, ".bar, :reset)

This can be easily mapped to HTML, XTerm, plain text, json, etc.

Updated by mame (Yusuke Endoh) over 1 year ago

Now error_highlight provides an API ErrorHighlight.spot(...) to take the column information programmatically, and Rails master uses it to show error_highlight in their code snippet. See https://github.com/rails/rails/pull/45818.

I believe that what kind of HTML fragment is needed is not up to error_highlight or the authors of exception classes, but up to each framework. For example, Rails already had a dedicated error page that displays the code snippet around where an error occurred. Even if error_highlight supports kinda full_message(highlight: :html) or something, it would not be so useful. So I do not plan to support this kind of thing in error_highlight.

Updated by ioquatix (Samuel Williams) over 1 year ago

I believe that what kind of HTML fragment is needed is not up to error_highlight or the authors of exception classes, but up to each framework.

That's a fair approach. But I'd prefer a simple interface for formatting HTML exceptions without having to sniff the version of Ruby or the features available. I'd like Ruby to be able to improve formatting and support new kinds of exceptions without the code that prints exceptions having to change. I'd be quite happy with exception.full_message(highlight: :html) for Rack for example. As your PR to Rails shows, the current implementation is non-trivial and requires a lot of work in the framework code.

Actions #3

Updated by jeremyevans0 (Jeremy Evans) over 1 year ago

  • Tracker changed from Bug to Feature
  • Backport deleted (2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0