ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "Eregon (Benoit Daloze)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:107149] [Ruby master Feature#18438] Add `Exception#additional_message` to show additional error information
Date: Sun, 16 Jan 2022 21:25:32 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-96001.20220116212528.18@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-18438.20211227064629.18@ruby-lang.org

Issue #18438 has been updated by Eregon (Benoit Daloze).


`detailed_message` maybe? (also the term used in https://bugs.ruby-lang.org/issues/18296#note-20)

IMHO `description` is fine and a good term in relation to `message`.
`full_message` is a weird name to me because it includes the backtrace and it's not really a "message" anymore, but that's how it is and seems unlikely worth to be renamed now that it is established.

----------------------------------------
Feature #18438: Add `Exception#additional_message` to show additional error information
https://bugs.ruby-lang.org/issues/18438#change-96001

* Author: mame (Yusuke Endoh)
* Status: Open
* Priority: Normal
----------------------------------------
## Proposal

I'd like to introduce a method `Exception#additional_message`, and let the Ruby error printer show it after `Exception#message`.

```ruby
class MyError < StandardError
  def message = "my error!"
  def additional_message = "This is\nan additional\nmessage"
end

raise MyError
```

```
$ ./miniruby test.rb
test.rb:6:in `<main>': my error! (MyError)
| This is
| an additional
| message
```

PoC implementation: https://github.com/ruby/ruby/pull/5351

## Motivation

At the present time, did_you_mean and error_highlight overrides `Exception#message` to add their suggestions.

```ruby
begin; 1.time; rescue NoMethodError; pp $!.message; end
#=> "undefined method `time' for 1:Integer\n" +
#   "\n" +
#   "  1.time\n" +
#   "   ^^^^^\n" +
#   "Did you mean?  times"
```

This implementation approach has a practical problem. Because it changes the return value of `Exception#message`, it breaks a test that checks the return value of `Exception#message`.
Though such a test is never recommended, I encountered some actual cases when creating error_highlight. See the change of tests of minitest as a typical example: https://github.com/seattlerb/minitest/pull/880/files

Currently, error_highlight shows hint information only for NoMethodError because it is relatively rare to check the message of `NameError`. Still, it broke some tests unfortunately, though. If possible, I'd like to add suggestions to other kinds of errors, but it will break much more tests.

If `Exception#additional_message` is introduced, and if did_you_mean and error_highlight overrides the method to add their suggestions, this problem will not occur because they no longer changes the result value of `#message` method.

## Cooperation needed

Currently, many Ruby/Rails users montiors their production services by using application monitoring services such as Sentry, DataDog, ScoutAPM, etc. The original motivation of error_highlight is for production (see #17930), so it will lose the significance if such services do not support `Exception#additional_message`. So, I'd like to hear opinions from developers of such services. If they are against this proposal or if we can't get their cooperation, I don't think my proposal should be accepted.

If you are a developer of these services, I would be very grateful if you could comment on this ticket. @ivoanjo

## Bikesheds

* I'm unsure if `Exception#additional_message` is a good name. Please propose alternatives if it is not good.
* Currently, the result of `addtional_message` is printed with no escape. This may be a more compatible solution against https://bugs.ruby-lang.org/issues/18367.
* It may be good to let `Exception#additional_message` accept `highlight` keyword as boolean information whether the output target is a terminal or not. Currently `Exception#full_message` accepts it. I have no plan to use the information in `error_highlight`, though. Not only `highlight` but also any keywords may be forwarded from `full_message(**opt)` to `additional_message(**opt)` for future use case.
* My current PoC adds prefixs "`| `" before each line of `addtional_message`. I'm unsure if this is good or bad. I'm happy to change or remove the prefixes.

---Files--------------------------------
截圖 2021-12-27 15.56.00.png (74.9 KB)


-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2022-01-16 21:25 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-27  6:46 [ruby-core:106833] [Ruby master Feature#18438] Add `Exception#additional_message` to show additional error information mame (Yusuke Endoh)
2021-12-27 16:07 ` [ruby-core:106838] " st0012 (Stan Lo)
2021-12-28  4:08 ` [ruby-core:106858] " mame (Yusuke Endoh)
2021-12-28 11:00 ` [ruby-core:106864] " ivoanjo (Ivo Anjo)
2021-12-29 11:59 ` [ruby-core:106899] " byroot (Jean Boussier)
2021-12-29 14:05 ` [ruby-core:106903] " mame (Yusuke Endoh)
2021-12-29 17:15 ` [ruby-core:106907] " Dan0042 (Daniel DeLorme)
2022-01-01 11:49 ` [ruby-core:106942] " Eregon (Benoit Daloze)
2022-01-06 19:31 ` [ruby-core:106988] " marcotc (Marco Costa)
2022-01-11  5:59 ` [ruby-core:107037] " nobu (Nobuyoshi Nakada)
2022-01-11 14:38 ` [ruby-core:107052] " Eregon (Benoit Daloze)
2022-01-11 16:20 ` [ruby-core:107055] " Dan0042 (Daniel DeLorme)
2022-01-13 17:35 ` [ruby-core:107103] " st0012 (Stan Lo)
2022-01-14  2:51 ` [ruby-core:107110] " mame (Yusuke Endoh)
2022-01-16 21:25 ` Eregon (Benoit Daloze) [this message]
2022-01-29  8:10 ` [ruby-core:107342] " mame (Yusuke Endoh)
2022-01-30 23:11 ` [ruby-core:107371] " Dan0042 (Daniel DeLorme)
2022-01-31 10:31 ` [ruby-core:107388] " mame (Yusuke Endoh)
2022-01-31 15:58 ` [ruby-core:107393] " Dan0042 (Daniel DeLorme)
2022-02-01 19:50 ` [ruby-core:107418] " mame (Yusuke Endoh)
2022-02-02  6:23 ` [ruby-core:107428] " mame (Yusuke Endoh)
2022-02-02 14:51 ` [ruby-core:107439] " Dan0042 (Daniel DeLorme)
2022-02-03  1:28 ` [ruby-core:107453] " mame (Yusuke Endoh)
2022-02-03  9:59 ` [ruby-core:107456] " st0012 (Stan Lo)
2022-02-03 10:38 ` [ruby-core:107458] " mame (Yusuke Endoh)
2022-02-03 14:06 ` [ruby-core:107468] " Dan0042 (Daniel DeLorme)
2022-02-03 20:55 ` [ruby-core:107475] " st0012 (Stan Lo)
2022-02-07  2:56 ` [ruby-core:107494] " mame (Yusuke Endoh)
2022-07-12  5:11 ` [ruby-core:109189] " mame (Yusuke Endoh)
2022-09-23  8:01 ` [ruby-core:110040] " mame (Yusuke Endoh)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-96001.20220116212528.18@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).