|Think about Loose Coupling|
Nice style for interesting error messagesby dash2 (Hermit)
|on Jan 24, 2001 at 21:14 UTC||Need Help??|
dash2 has asked for the
wisdom of the Perl Monks concerning the following question:
I'm writing a module to do WHOIS lookups on domain names. The actual WHOIS lookup is relatively unusual for the following reason. Normally, interesting values are returned on the success of some method. Failure is uninteresting. So we use 0 or undef for failure. In a WHOIS, success is uninteresting: if a domain name is available, it's available. Failure, OTOH, is interesting: is the domain already bought? Or the whois server didn't respond? Or you couldn't contact the server? Or some internal script error?
So my question is, what is a nice way to handle "interesting" errors? I can think of a few:
1) Have failure return true (with an error message) and success false. Elegant, but counter-intuitive.
2) Have the subroutine/method return two values: success/failure, and an optional error message. Straightforward, but that extra value is going to waste space most of the time, and it's a bore to write your method calls always to expect two values.
3) Return true/false as normal for success/failure, and write to an object variable for the error message: e.g.
Opinions would be interesting. TMTOWDI and all that, of course, but I'd like to hear some views on the right way in particular situations.