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

Improve enum fromOrdinal and valueOf error messages #19178

Closed
Lasering opened this issue Dec 3, 2023 · 5 comments · Fixed by #19182
Closed

Improve enum fromOrdinal and valueOf error messages #19178

Lasering opened this issue Dec 3, 2023 · 5 comments · Fixed by #19182
Assignees
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:enhancement

Comments

@Lasering
Copy link

Lasering commented Dec 3, 2023

Compiler version

3.3.1

Minimized example

enum Foo:
  case A

Foo.fromOrdinal(3)
Foo.valueOf("Bar")

Output Error/Warning message

fromOrdinal

java.util.NoSuchElementException: 3

valueOf

java.lang.IllegalArgumentException: enum case not found: Bar

Why this Error/Warning was not helpful

It does not state from which enum the error has originated, making it harder to track it down.

Suggested improvement

fromOrdinal:

java.util.NoSuchElementException: enum `Foo` has no case with ordinal: 3

valueOf

java.lang.IllegalArgumentException: enum `Foo` has no case with value: Bar
@Lasering Lasering added area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:enhancement stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 3, 2023
@nicolasstucki nicolasstucki self-assigned this Dec 4, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Dec 4, 2023
@bishabosha
Copy link
Member

bishabosha commented Dec 4, 2023

sure we can change the message but is looking at the stack trace no good?

@Lasering
Copy link
Author

Lasering commented Dec 4, 2023

Sure the stack trace helps, but why make it harder, specially for newbies? This way you just look at the error message and you get all the information you need.

The message could be even more helpful stating the possible expected values.

@hamzaremmal hamzaremmal removed the stat:needs triage Every issue needs to have an "area" and "itype" label label Dec 4, 2023
@som-snytt
Copy link
Contributor

You could "split the difference" and throw IndexOutOfBoundsException for both methods. (joke)

Enumeration threw NoSuchElement for lookup by name, like Map#apply but once omitted even the name scala/scala#4229

java.lang.IllegalArgumentException: No enum constant java.lang.Thread.State.foo

I guess the full name is usefuller.

Listing elements could be very verbose.

@Lasering
Copy link
Author

Lasering commented Dec 4, 2023

Pretty sure this does not make sense since these are runtime errors, but is it possible to show the elements based on some compiler verbose flag?

@som-snytt
Copy link
Contributor

In the discussion over whether case class toString can be made verbose, I proposed a compiler flag to enable it, with the idea that during development you want more verbose output always.

That is an easier sell if the compiler had a native notion of dev vs prod target. Flag fiddling at the build tool level is more fraught.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants