13.7. Error Messages
An error message is nearly useless if it's unintelligible to those who encounter it. For example, someone who uses a subroutine to load DAXML data[*]:
use XML::Parser::DAXML qw( load_DAXML );
my $DAXML_root = load_DAXML($source_file); will want to see an error message like this:
File 'index.html' is not valid DAXML.
Missing "</BLINK>" tag
Problem detected near "</BLINK</HEAD>".
Failed at 'DAXML_to_PDF.pl', line 3 An error message like that indicates what the overall problem is (not valid DAXML), why it was considered a problem (Missing "</BLINK>" tag), where the problem occurred (File 'index.html', near "<BLINK</HEAD>"), and which line in the caller's source failed ('DAXML_to_PDF.pl', line 3). Collectively this informationwhat's wrong, why it's wrong, where in the data, and whence in the codemakes it easy for those who are using your utility to locate and correct their own problems. Unfortunately, most exception messages are written by developers, and for developers (i.e., themselves). Most often, they're written during the testing or debugging process, so they tend to be written in the language of the developers, using the vocabulary of the implementation. So someone who is using your utility is likely to be confronted with an error message like: Invalid token ('<') at 'Acquisition.pm', line 2637 This is very concise (one-fifth the size of the error message suggested earlier) and completely accurate (the problem is indeed the unexpected < of the </HEAD> tag buried in the incomplete </BLINK tag). But it's likely to be of very little help to those who are using your module. They may have no idea what a parser token is, they're faced with thousands of angle-brackets in their data, and they certainly don't want to look through several thousand lines of your module source to try and work out what they did wrong. So, don't throw exceptions with curt messages that are expressed in the terminology of the implementation: # Otherwise parsing has failed... else { # So report the guilty token... die qq{Unmatched token ('$start_token')}; } Instead, always croak with a detailed message. And phrase it in the vocabulary of the problem space, using concepts that will be familiar to the caller:
![]() |