Error messages in Parsica give you information about what the parser expected, where the problem happened, and what it got instead.
(Note: We're using UpToDocs to automatically test all the code samples in this documentation. The downside is that throwing an exception in the docs causes the build to fail! That's why some of the code above is commented out.)
If you uncomment and run the above code, you'll get an exception like this:
It shows the filename, line number and column position, as well as an autogenerated expectation.
Often you'll want something a bt more meaningful than "at least one A-Z or a-z". You can do that by attaching your own labels to some of your parsers. For example, we can label the colours:
That will yield:
Or you can attach the label to the entire parser:
The best approach will of course depend on your specific use case. A good habit is to keep in mind that ultimately, the errors are there for the end user who will see them. This could be a user who enters some values in a form, a programmer using your API, someone building other parsers on top of your parser... Feed your parser with some wrong inputs that you are likely to get in the real world, and get a feel for what makes a helpful error message.
Doing your own error reporting
The information in the error message is also available from
ParseResult. You can use this to make your own error messages, if you want to render them as HTML or send them to an API. For example, these are the line and column number where the parser ended up:
Have a look at the
ParseResult API to see what else it can do.