in reply to Perl Style: About error messges opening files

I agree with Abigail that it definitely matters who's going to see this message. If it's going to show up in an error log, I just make sure it's a unique string that I can grep for easily, something like this:

foo or die "sorrow, pestilence, Notepad: $!$/";

As long as I don't use the same error string twice, it takes about two seconds to find the place where the problem is.

If it's an error message that an end user is going to see, I do something rather different...

open FOO, "<$foo" or fatalerror "Couldn't Read Foo: $!"; # and off in an include file somewhere I define... sub fatalerror { my $email = $ENV{SERVER_ADMIN}; use HTML::Entities; my $code = encode_entities(shift); my $fm = <<"FRIENDLYMESSAGE"; <p class=\"error\">Oops! I seem to have run into a problem! This is probably a bug in the software on this site, something that the webmaster needs to fix. If this problem persists, you can contact the webmaster at <a href="mailto:$email">$email</a> and report the problem. It might get fixed faster if you include the exact technical error code that follows. </p> <p class=\"error\">Technical Error Code: $code</p> FRIENDLYMESSAGE # And then I output a page containing $fm in the # usual fashion. }

This is for CGI, which in my case is the only kind of interface used for stuff that end users see, but you can imagine a similar sort of thing for GUI stuff.


;$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$;[-1]->();print

Replies are listed 'Best First'.
Re: Re: Perl Style: About error messges opening files
by demerphq (Chancellor) on Apr 27, 2004 at 21:12 UTC

    I have a logging module that "plugs in" to perl. Anything (or nearly anyway) going out on STDERR or STDOUT, even via die or warn, is automatically captured and logged with the time it happened, the line of code responsible, what "channel" was used (ie STDOUT or STDERR) and a few other bits and bobs, some automatic and some provided by the author of the script using the module. It also send automatic success/failure emails including a full stack backtrace and running enviornment of the script (in the case of error anyway.) The result of all this is I havent had to grep a logfile for a specific message in a long time. When something I wrote or maintain crashes I get an email with all the relevent details. If I need to review the logfiles to see what else was happening at the time I have alot more to go on than the fact that I was sufficiently creative with my message such that it was unique in the script. Which I know from experience is not a given at all. :-)

    My point here is that if you are using proper logging then unique error messages and the like are unnecessary. If its going to be an end-user-facing error message of course this is different. But as I said elsewhere in this thread virtually all of my code runs as background tasks and pretty error messages arent called for.

    I actually have permission from my boss to release the module, but ive never had the time to clean it up and make it more portable. Its somewhat Win32 biased, with hooks for the event log and other win32 tricks, and since the equivelent Unixy bias annoys me (because I cant play with the toys) id rather not be responsible for the opposite.

    BTW, I assume your use of $/ is only for brevity... Ive been bitten once too often by that trick to think its particularly useful out of one liners.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


Re: Re: Perl Style: About error messges opening files
by Juerd (Abbot) on Apr 28, 2004 at 10:07 UTC

    If it's going to show up in an error log, I just make sure it's a unique string that I can grep for easily

    Ehm. I always thought the filename and line number included in the error message (if you don't let it end in \n) made that unnecessary. They're not scary, not even to beginners. Beginners learn to ignore these things the minute they start using a unixish system, developers learn to look at the line mentioned. That way, you increase the chance of getting a patch with the bug report if it happens to be a bug.

    Besides, going to a certain line number is much faster and easier than grepping or searching.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      using a random uniq message allows you to easily change the message to something better later on.

      s/mumble foo:/missing input file:/ s/bonk:/unknown request:/ s/what the frell:/data base seems broken:/
      going to a certain line number is much faster and easier than grepping or searching

      Not in my experience. A line number is harder to hold in your head, so you probably have to copy-and-paste it from wherever (browser, email, log, ...) into the jump-to-line feature of your text editor. As opposed to just hitting ctrl-S and typing the first N characters of a phrase. By the time you get to the second word, you're there usually there.

      I always thought the filename and line number included in the error message

      If you're getting the error message from a logfile or at the command prompt maybe. If an end user sees the message and tries to remember it to tell you, the chances of a full filepath coming through accurately, much less a line number, are vanishingly close to zero. A phrase like "Twinkies and Spandex" is significantly more likely to get to you intact.


      ;$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$;[-1]->();print

        A line number is harder to hold in your head

        When you have 852442 line files, maybe, but I'm perfectly capable of remembering 3 digits for a short time. I'm sure that if you try, you can also do that. Sometimes, but not often, there are 4 digits. Still not at all hard.

        If an end user sees the message and tries to remember it to tell you

        Error messages are sent to me by email. The one time per week that someone actually calls, they're always reading it off the screen. Perhaps you code for the internetless?

        A phrase like "Twinkies and Spandex" is significantly more likely to get to you intact.

        bar.txt: No such file or directory at foo.pl line 15
        versus
        Twinkies and Spandex (No such file or directory)
        Something tells me you're wasting my time.

        Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }