Moose Loose Ends Part the Second

Still error day here in the Moose Pen.

As I am a little lazy today and I didn't have time to work on this project last night, there was a Godzilla film festival on last night, it just a quick postette today.

I went though all the Database::Accessor code today and cleaned up at least the format of my die calls.

Looking about at a number of other applications I figure I might as well go with the semi-standard CME or 'Class', 'Method', 'Error' style.

Lets take this first example the Die that one could run into and that is found in the BUILD sub of Database::Accessor when it call out to the '_loadDADClassesFromDir' sub;


opendir( DIR, $path ) or die "Unable to open $path: $!";

Not one of my better error messages so I changed it over to;

opendir( DIR, $path ) or die "Database::Accessor BUILD Error: Unable to open $path: $! during load of DADs";

The next error message I encountered posed a little more of a problem

my %saved = %$self;
tie(
%$self,
"MooseX::AccessorsOnly",
sub {
my ( $who, $how, $what ) = @_;
die
"Database::Accessor Error: Attempt to access '$what' directly at $who!";
}
);
%$self = %saved;

the above it the majick invocation code to get 'MooseX::AccessorsOnly' working. If you remember that stops someone from doing this

use Xtest::DA::Person;
my $person = Xtest::DA::Person->new();
my $da = $person->da();
push(@{$da->{elements}},{name=>'salary',view=>'people'});

and one would get an error like this now;

Database::Accessor Error: Attempt to access 'elements' directly at main:4 at GitHub\database-accessor\lib/Database/Accessor.pm line 197.

Now the only problem with the above is that I did not try to do that push as line 197 in Accessor.pm but at line 4 of the above program. Using 'die' here is not much help to my end users as they will most likey just send in a but report for the above.

I could split out the '4' out of the $who and then user the 'caller' function to get who made the call but I would still have that 'at GitHub\database-accessor\lib/Database/Accessor.pm line 197' at the end of the error.

Far better me thinks it to try and use our good old friend 'Carp' to modify what is coming out. To start I swapped out that 'die' for a 'Carp::croak' and got


Database::Accessor Error: Attempt to access 'elements' directly at main:4! at
/Perl64/site/lib/MooseX/AccessorsOnly.pm line 58.

Well not much better that a 'die' and so I tried 'Carp::confess' and got

Database::Accessor Error: Attempt to access 'elements' directly at main:4! at D:\GitHub\database-accessor\lib/Database/Accessor.pm line 196.

Database::Accessor::__ANON__("main:21", "FETCH", "elements") called at D:/Perl64/site/lib/MooseX/AccessorsOnly.pm line 58

MooseX::AccessorsOnly::__ANON__(MooseX::AccessorsOnly=HASH(0x5fd89e0), "elements") called at i_error.pl line 4


Ok lots more stuff not as much a 'Moose Poop' but at least I know where the call starts from and I can link that line 4 up with the last line on the error message.

There we have today's postette, as the rest of the code I did today was either cleaning up the verbiage on an error message or converting a 'die' over to a 'Carp::confess' .

The lessons for today;


  1. Use 'die' when something goes horribly wrong and Database::Accessor cannot load or run.

  2. Use 'Carp::confess' when the Database::Accessor is up and running and the end user does something to cause an error.

moosemash.png

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations