I think this is a misinterpretation on your side.
Chromatic is not arguing against string comparisons per se, but against converting complex structures (errors) into strings which then need to be parsed back into complex structures in other places.
And I think that misinterpretation lies elsewhere. I do understand Chromatic's point, but I think my point is equally valid. If not stronger as it happens earlier in the scheme of things: that is, earlier in the execution order.
Take this
use Exception::Class (
'MyException',
^^^^^^^^^^^
+ #1
'AnotherException' => { isa => 'MyException' },
^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
+ #2 & 3
'YetAnotherException' => {
^^^^^^^^^^^^^^^^^^^^
+ #4
isa => 'AnotherException',
^^^^^^^^^^^^^^^^
+ #5
description => 'These exceptions are related to IPC'
},
'ExceptionWithFields' => {
^^^^^^^^^^^^^^^^^^^
+ #6
isa => 'YetAnotherException',
+ #7
^^^^^^^^^^^^^^^^^^^
fields => [ 'grandiosity', 'quixotic' ],
alias => 'throw_fields',
},
);
There are seven, unchecked points of failure in that small sample. Those strings eventually end up in the symbol table, which is a hash.
Later, when the exceptions are thrown; and later still when they are caught and isolated, a lookup into symbol table will be done to find them--and that is a string comparison.
If there are typos in any of those 3 places; or if the exact name of one of them is changed, accidentally or deliberately at some later point in the development or maintenance cycle; then a mismatch occurs. But because they start life as strings, those errors won't show up until runtime. As these are by definition, only exercised in exceptional circumstances, that could be months or even years later before the mismatch is discovered in production.
Now, as this is Chromatic we're talking about, I'm sure--had he not disengaged(*)--that he would argue that those failure paths should be exercised during pre-production unit & systems testing. And he would be right.
But exactly the same holds true for any failures in the parsing of exception strings! In both cases, either the code is tested and errors are caught before production; or they're not and they turn up in production.
And when the mismatch is detected, there will be just as many places to change in the E::C class code as in the string error code--assuming equal diligence to the DRY principle on behalf of both developers.
E::C is really only one way to do it.
That was the original basis of my contention. eval & die is a legitimate other way; that has been acceptable to Perl programmers for the best part of 20 years; and is used, where needed, by 18,000+ (-60 that use E::C) CPAN modules.
I've no objection to people re-inventing the wheel, but mandating change for change's sake, is intolerable.
Recommending a heavy & broken(see below) replacement, even worse.
Just plain throwing hashes as exceptions makes things nicer
That's fine--if you actually need complex structure--which is debatable. But does no one see how easy and lightweight that can be done:
eval{ die { a=>b=>c=>d=>}; } or print %{ $@ };;
c d a b
because you can add things to them without any risk of breaking regexes
I think that I've demonstrated above that the "risk" is no greater that with E::C, if you assume equally diligent developers and testing regimes. To assume otherwise is either naive or prejudicial.
when converting back to a complex structure on inspection
And this is the final sinew on my bone of contention. Why do you need structured information to deal with an exception?
If you are going to deal with by trying to 'fix' it, then you will (should) be at a point in the code where you already have access to all the required information to do so. There should *never* be an occasion when the failing routine has to pass you information to deal with an exception that you don't already have access to. After all, you called the routine and passed it the relevant information!
And if you are going to deal with it by reporting an error; you only need to log the string form anyway. The most you need to do is prefix it with something like:
An error occurred.
The following information probably won't mean anything to you, but it will greatly help our developers to provide you with a quick solution. Please report it accurately.
blah...
Or, if you feel the need to keep the technical details from your customers, display a "user friendly" message of the form:
An error has occured. Please pass the full details, from error message timestamped: "yyyy-mm-dd hh:mm::ss Error123" in the program log, verbatim to our developers, along with as much other information as possible, for a speedy resolution.
and log the text of the exception.
Most of the time, I see no technical reasons to extract complex information from an error message. And that really squashes the main argument for structured exceptions. Which makes mandating their universal use nothing more than jumping headlong on the Java-esque bandwagon and pandering to the latest fad.
As far as a workable example goes, what would be wrong with this: http://gist.github.com/616705
Um...apart from that it wasn't available until 5:30am today, which means my searches--nor anyone else's--could have found it at the point I did my searches; it uses Try::Tiny. Which may be a good way to do it, but isn't mentioned in the E::C synopses, and essentially throws away the purpose of half the code including two of the dependencies of E::C.
The module synopsies use: # catch
if ( $e = Exception::Class->caught('MyException') ) {
warn $e->error, "\n", $e->trace->as_string, "\n";
warn join ' ', $e->euid, $e->egid, $e->uid, $e->gid, $e->pid, $e
+->time;
exit;
}
elsif ( $e = Exception::Class->caught('ExceptionWithFields') ) {
$e->quixotic ? do_something_wacky() : do_something_sane();
}
else {
$e = Exception::Class->caught();
ref $e ? $e->rethrow : die $e;
}
Which to the best of my abilities, simply doesn't work!
(*) And I take his early disengagement from this discussion, to mean that these realities have dawned upon him also.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|