http://qs1969.pair.com?node_id=493972


in reply to Style guide for error messages?

My practices are these.

I always make sure that it would be clear from the error message that it's an error. I don't use a single prefix, just include one some word like qw"error cannot failed failure". It can be very confusing if you write

read $FOO, $s, $l or do { warn "reading foo file"; last PROCESS_FOO; }
because it will seem to be a status message saying the program is starting to read the foo file.

I always include $! if its value is meaningful (that is, if the function that fails sets it).

I always provide the implicit line number (although I almost always ignore it when I read the error message). The exception is some status messages that are not errors.

I don't capitalize the error message.

I sometimes give details about the error, sometimes not. I add the details in the die statement if the error actually happens and I don't understand why.

If you want to know more about my style, here is the complete (ok, I could have missed some) list of error statements in two of my programs.

1 < ($scope = 0 + $scope) or die "nearword scope must be greater than 1"; open $file, "<", $filename or die "cannot open text file for nearword: $!"; $nl <= $n && $n <= $nle or die "postcondition failed"; @a or die "internal error: quantile1 called on empty + list, n=" . $n; @{$hheap[1]} and # DEBUG die "internal error: couldn't pop non- +empty hheap"; $used{$q}++ and warn "single-link internal: used hash corrupt" +; @order == @point && @gap == @order or die "singlelink internal error: lost or duplicate elem +ents: order " . @order . ", point " . @point . ", gap " . @gap; keys(%$hash) == @$heap or die "assertion failed: hheap size mismatch: hash " . k +eys(%$hash) . ", heap " . @$heap; for $k (0 .. @$heap - 1) { ref($$heap[$k]) eq "ARRAY" or die "assertion failed: non-array heap element +index " . $k; exists($$hash{${$$heap[$k]}[1]}) or warn("assertion failed: hheap element missing +from hash: "), die "index " . $k . ", element " . ${$$heap[$k]}[1] . +", distance " . ${$$heap[$k]}[0]; $k == $$hash{${$$heap[$k]}[1]} or warn("assertion failed: hheap index mismatch: +"), die " real index " . $k . ", element " . ${$$heap[$k]}[1] . ", hash value + " . $$hash{${$$heap[$k]}[1]}; ${$$heap[($k - 1) >> 1]}[0] <= ${$$heap[$k]}[0] or warn("assertion failed: hheap heap mismatch: " +), die "parent index " . $k . ", element " . ${$$heap[$k]}[1] . ", di +stance " . ${$$heap[$k]}[0] . ", child index " . (($k - 1) >> 1) . " +, element " . ${$$heap[($k - 1) >> 1]}[1] . ", distance " . ${$$heap[($k - 1) >> + 1]}[0]; 1 == @ARGV or die "Usage: rock-cut -g goal hierarchy.xml"; length($id) or die "invalid input: element with no id"; exists($element{$id}) and die qq[invalid input: duplicate element id "$i +d"]; length($id) or die "invalid input: cluster without id"; exists($cluster{$id}) and die qq[invalid input: duplicate cluster id "$i +d"]; @root or die(@root ? "error: no root clusters found -- wierd" : + "error: no clusters found at all"); exists($$d{"weight"}) and do { if (defined($$d{"weight"})) { warn qq[warning: cluster included in + more than one cluster: "$n"]; } else { die qq[error: circular clusters: "$n +"]; } }; { warn qq[cluster "$c" has non-existen +t child "$cd"]; next; }; defined($mi) or do { warn "clusters fragmented totally, decrease go +al or outlier_max"; last; };