On Mon, Apr 05, 2010 at 05:08:24PM -0700, Buddy Burden wrote:
> Moosites,
>
> This is a request from a co-worker; I'll see if I can explain it well.
>
> Let's say I have a simple Moose class:
>
> use Company::Moose; # this module gets me MooseX::Declare, and also
> declares all my class types, like the one I'm using below
>
> class Foo
> {
> has 'bar' => ( is => 'ro', isa => Int);
> has 'baz' => ( is => 'ro', isa => Company::Thingy );
> };
>
> So now if I later do this:
>
> my $foo = Foo->new(bar => "I'm not an int!", baz =>
> Company::DifferentThingy->new );
>
> then my code will fail with a run-time error. Which is generally what I want.
>
> But suppose I want to collect these validation errors and return them
> in some more useful fashion? I could always wrap my call to new() in
> an eval, of course, but then I'd have to parse the "internal
> validation error" out of $@, which seems yucky. Basically what I want
> is to return some list of errors back to my caller, perhaps a web
> page, saying "bar wasn't an integer, and baz wasn't a Thingy" so that
> then my caller can hopefully do something about that (e.g. notify the
> user that their input was invalid). Is there some trickery I can do
> with BUILD or BUILDARGS or somesuch to achieve this fairly easily?
>
> TIA for any help.
>
>
> -- Buddy
You probably want
http://search.cpan.org/dist/MooseX-Constructor-AllErrors/
-doy