Customize Assertion Error Messages
How can I add additional context to an assertion error's message?
Last updated
How can I add additional context to an assertion error's message?
Last updated
Instead of trying to provide facilities to fully customize the error message, Moat Maker strives to simply deliver good and helpful error messages. If you feel a particular error message could be improved in some way, please and let us know.
That being said, it's still important to provide ways to add additional context to an error. For this reason, two different parameters have been made available to give you this power: at
and errorPrefix
. To show how these behave, let's start with a very simple validator:
If we call .assertMatches()
without customizing the error at all, we'll be given the following error message:
Using errorPrefix
, we can add arbitrary information to the start of the error string. Note that the errorPrefix
string provided absolutely must end with a colon.
We can also provide a custom at
field to change the default <receivedValue>
text to be whatever we want. This serves two purposes. 1. You can use something more helpful than the extremely generic "receivedValue" text. 2. If you're validating a value that's part of a deeper object structure, you can provide information about the path already taken to get to where you are. This example illustrates both of these use cases.
For the common scenario of validating user input for your public API, a special .assertArgs()
function is provided that's pre-configured to provide appropriate "prefix" and "at" fields for you. For the most descriptive errors, it's best to use .assertArgs()
with a validator that matches against , however, any validator can be used.
.assertArgs()
takes two parameters. The first is the name of the function we're validating the input for. Some examples might be "getName()"
, "MyClass.myStaticMethod()"
, "<date instance>.getDay()"
, or whatever descriptive name you can think of to help someone receiving this error know where the issue happened at. The second argument should be the complete list of arguments your input function takes. You can conveniently use to accomplish this purpose. If you do not wish to use the arguments object, or if you can not (e.g. because you're using an arrow function which does not support it), you can instead choose to use an alternative pattern, like spreading and destructuring:
Be warned that enabling this pattern can make it harder to make non-breaking updates to your library. If, later, you want to allow your function to accept additional arguments, you might not be able to do so, because your users may already be passing in additional throw-away arguments. For this reason, it is discouraged to ever provide extra arguments to a function. If interested, related to this topic, which ends with a JavaScript committee member stating that developers should avoid this practice for this same reason.
By default, a TypeError is thrown when an assertion fails. If you need to throw a different error, you can use the errorFactory
parameter. See the for more details.