Agoric / nat

Ensures that a number is within the natural numbers (0, 1, 2...) or throws a RangeError
Apache License 2.0
5 stars 4 forks source link

improve error message? #66

Open warner opened 5 years ago

warner commented 5 years ago

When Nat() rejects the argument, it throws RangeError("not a safe integer"). I'm finding it a bit frustrating that the error message doesn't tell me what is being rejected (RangeError("'undefined' is not a safe integer")), and for debugging, I wind up needing to wrap the Nat call with:

try {
   Nat(thing);
} catch(e) {
   throw new Error(`Nat(${thing}) failed: ${e.message}`);
}

which kind of loses the succinctness of Nat(thing).

Would it be appropriate to change the error messages to include the stringification of the rejected non-natural-number?

OTOH, I can imagine some benefits to having a predictable (and string-matchable) error message. I don't know what the JS convention is on examining error objects.

erights commented 5 years ago

We could also add an optional second parameter, which, on error, is stringified and added to the message.

katelynsills commented 4 years ago

@warner slightly better error messages were added in v3.0.0. Do you think it solves this issue or should it remain open?