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


mame (Yusuke Endoh) wrote in #note-5:
> Because we don't inspect an exception object so often, I think the redundancy is not much of a problem. I somewhat agree with the concern about incompatibility. Though it is not very admirable to depend on the return value of `#inspect`, some changes of tests are actually needed for my PR.

Right, the #inspect value should generally not be relied upon, but I feel something like `rescue => e; p e` or `log e.inspect` might not be so rare, especially when there was no `Exception#full_message` (or users might not know about it).
Or even as a simple way to include the cause like `rescue => e; raise FooException, "some error (#{e.inspect})`.
`Exception#to_s` is not useful as it only returns the message.

Yet another case is in IRB:
```
> e
=> #<RuntimeError: my message>
```
is nice and I think what people expect.
```
> e
=> #<RuntimeError: "my message">
```
not so much.

So I think 1 or 3 is better.

Of course there is no guarantee general `inspect` returns a single-line, so it seems best if usages of Exception#inspect or Exception#message handle multiple lines correctly.

----------------------------------------
Bug #18170: Exception#inspect should not include newlines
https://bugs.ruby-lang.org/issues/18170#change-93842

* Author: mame (Yusuke Endoh)
* Status: Assigned
* Priority: Normal
* Assignee: mame (Yusuke Endoh)
* Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
Is this intentional?

```
p StandardError.new("foo\nbar")
#=>
# #<StandardError: foo
# bar>
```

I expect `#inspect` returns a one-line string. How about returning `#<StandardError: "foo\nbar">` or something?

Recently, multi-line error messages have been increasing by the introduction of did_you_mean and error_highlight. Printing an object that contains such an exception leads to a tricky output:

```
class Foo
  def initialize
    @exception = begin; exampl; rescue Exception; $!; end
  end

  def example
  end
end

p Foo.new
#=>
# #<Foo:0x00007f15aeb4ba48 @exception=#<NameError: undefined local variable or method `exampl' for #<Foo:0x00007f15aeb4ba48 ...>
#
#     @exception = begin; exampl; rescue Exception; $!; end
#                         ^^^^^^
# Did you mean?  example>>
```

This issue was originally found by @ioquatix 



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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>