Sub topic 15
Argument Validation and When Errors Are Thrown
This chapter assumes you know how to create iterators with .values() or generators, and that helpers are lazy: their callbacks run only when next() pulls a value. Laziness covers the work a helper does. It does not cover checking the helper's arguments. Helpers check their arguments as soon as you call them, before any value is pulled.
Two Kinds of Errors
Errors from an iterator pipeline come in two kinds, and they appear at different times:
| Error source | When it is thrown | Typical type |
|---|---|---|
| Bad argument to `take` or `drop` | When the helper is called | `RangeError` |
| Non-function passed to `map` or `filter` | When the helper is called | `TypeError` |
| Exception inside your callback | When `next()` reaches that value | Whatever you threw |
How take and drop Check Their Limit
take(limit) and drop(limit) first convert limit to a number. If the result is NaN, they throw a RangeError. Otherwise they truncate it to an integer, and if that integer is negative they throw a RangeError. Infinity is allowed.
for (const bad of [-1, NaN, 'abc']) {
try {
[1, 2, 3].values().take(bad);
} catch (e) {
console.log(String(bad), e.name);
}
}
// -1 RangeError
// NaN RangeError
// abc RangeErrorValues that convert cleanly are accepted without complaint, and this can hide bugs:
console.log([1, 2, 3, 4].values().take(2.9).toArray()); // [1, 2]
console.log([1, 2, 3, 4].values().drop('1').toArray()); // [2, 3, 4]
console.log([1, 2, 3].values().take(Infinity).toArray()); // [1, 2, 3]Callback Checks Versus Callback Errors
map and filter check right away that their argument is callable. An exception thrown inside a valid callback stays hidden until that value is pulled:
try {
[1].values().map(42);
} catch (e) {
console.log(e.name); // TypeError
}
const it = [1, 2].values().map((x) => {
if (x === 2) throw new Error('bad value');
return x;
});
console.log(it.next().value); // 1
try {
it.next();
} catch (e) {
console.log(e.message); // bad value
}The first next() works fine. The error appears only when the pipeline reaches 2.
Why Eager Validation Helps
When an argument check fails, the helper throws where you built the pipeline, so the stack trace points at the broken line of code. If the check waited until iteration, the error could surface much later, inside a for...of loop in some other module. Eager checking also means an invalid take(-1) on an infinite generator throws before the generator body runs at all.
Exercise
Which line throws, and what type of error: const p = gen().map((x) => x.id).take(-5); followed later by p.toArray();, where gen yields objects?
Show answer
The first line throws a RangeError as soon as take(-5) is called. p is never assigned, so toArray never runs and the map callback never executes.
Exercise
What does [10, 20, 30].values().take('2.5').toArray() return?
Show answer
[10, 20]. The string '2.5' converts to 2.5, which is truncated to 2. Because 2 is not negative and not NaN, no error is thrown.