dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.57k forks source link

String.substring(x, 1) leads to confusing message when x > 1 #28966

Open Hixie opened 7 years ago

Hixie commented 7 years ago

When the start index is greater than the end index, we throw a RangeError with the start index as the value that's flagged as bad. This leads to an error like "RangeError: Value not in range: 97".

This is confusing because the start index is fine, what's wrong is the relationship between the start and end indices. (Typically this will happen when the developer thinks the second argument is a length, rather than an end index.)

IMHO we should through a more elaborate ArgumentError here rather than a RangeError.

lrhn commented 7 years ago

There are better RangeError versions too. I'm making the VM use RangeError.checkValidRange and dart2js do better errors too (and some flow optimization while I'm there).

Hixie commented 7 years ago

Similar problem with double.clamp(). I had accidentally given it double.INFINITY for the first argument (instead of -double.INFINITY) and it just told me that wasn't a valid value, not that the lower bound was greater than the upper bound, which would have been much more helpful.