tags:

views:

93

answers:

1

I just recently started out on Moose and its a great OO framework not only to use but also to learn new OO concepts. One of the things I wanted to do was to do error reporting from perspective of caller during object creation. I saw that Moose has the module Moose::Error::Croak which tells Moose to override the default error reporting by croak call. I used it but it did not seem to help

Moose code - Foo.pm

package Foo;
use metaclass (
  metaclass => 'Moose::Meta::Class',
  error_class => 'Moose::Error::Croak',
);
use Moose;
has 'attr1' => (
  is => 'rw',
  isa => 'Str',
  required => '1',
);
no Moose;
1;

Moose code - fooser.pl

#!/usr/bin/perl
use strict;
use warnings;
use Foo;
my $foobj = Foo->new();

This fails with error: Attribute (attr1) is required at /usr/local/lib/perl/5.8.8/Class/MOP/Class.pm line 364

which is terse than the actual stack trace if Moose::Error::Croak is not used. But it does not report it from perspective of caller. If this were a Perl 5 OO code and I had Foo.pm as:

package Foo;

use strict;
use warnings;
use Carp;

sub new { 
  my ($class, %args) = @_;
  my $self = {};
  if (! exists $args{'attr1'}) {
    croak "ERR: did not provide attr1";    
  }
  $self->{'attr1'} = $args{attr1};
  bless $self, $class;
  return $self;
}
1;

And if fooser.pl was executed I would have got the error:

"ERR: did not provide attr1 at fooser.pl line 6"

which is from the perspective of the caller as it points to line no. 6 of fooser.pl rather than MOP.pm's line no. 364.

How can I do this in Moose? Or am I misunderstanding something here?

+3  A: 

One option is to use MooseX::Constructor::AllErrors.

This module collects constructor errors and prints them all together (default behaviour is to die as soon as the first error is encountered). It has a side-effect of printing from the caller's perspective.

Replace your use metaclass with use MooseX::Constructor::AllErrors; and the error becomes:

Attribute (attr1) is required at ./fooser.pl line 5

However, if you call Foo->new from inside another module (e.g. Bar) and call Bar->new in your script, the error would be more like:

Attribute (attr1) is required at Bar.pm line 8

...so it's not a complete solution.

rjh
Thanks for pointing that out rjh. Although, Bar.pm does not work as intended as per your post, at least a direct call should give the desired behavior.
Saurabh Hirani