--- On Mon, 9/22/08, vincent roudil <[EMAIL PROTECTED]> wrote:

> From: vincent roudil <[EMAIL PROTECTED]>
> Subject: how to handle error with type constraint
> To: moose@perl.org
> Date: Monday, September 22, 2008, 9:40 AM
> Hello Moose,
> I am just starting to explore Moose. This is probably
> something simple, but
> I can't figure out the best way to do this.
> 
> With the Moose type constraint, I would like to get control
> of the error
> handling when the type is invalid.
> 
> Here is an example.
> 
> a class host, with 2 attributes, name and ip_addr, with a
> control on the IP
> address validity:
> ##### package host.pm
> package host;
> use Moose;
> use Moose::Util::TypeConstraints;
> 
> use Regexp::Common qw /net/;
> subtype IPAddr
> => as Str
> => where {/^$RE{net}{IPv4}$/}
>  => message { 'invalid IP address'};
> 
> has 'ip_addr' => (isa => 'IPAddr', is
> => 'ro', required => 1);
> has 'name' => (isa => 'Str',      is
> => 'ro', required => 1);
> 1;
> ##### end package host.pm
> 
> The type constraints works fine. Only that when the IP
> address is invalid
> the program dies, with great verbosity.
> 
> I would just like when IP address is invalid, to print a
> message in a log
> file, and that the creation of the instance fails quietly,
> so I could use
> the host class like this:
> 
> ##### main.pl:
> use host;
> 
> my $h=host->new(name=>'jupiter',
> ip_addr=>'10.10.10.1');
> 
> if ($h) {
>  print $h->name." created successfully with the IP
> address ".$h->ip_addr;
> }
> 
> If someone could point me to the right direction, that
> would be great.
> 
> Thanks in advance.
> 
> Vincent.

Hey,

For me I would try to clearly separate my business logic validation needs from 
type constraints on my storage classes or domain model.  There are a few 
reasons for this, mostly having to do with separation of concerns and the fact 
that incoming user data will likely have a few different validation paths at 
some point.  Also you can create reuseable validation classes.

I don't know your exact problem domain, but for me as a web developer I am 
doing a lot of validation and error messaging for incoming post params.  For 
that I use Data::FormValidator, and make as many custom classes as I need.  
Then I'd try to have some sort of workflow on the incoming params to first 
validate and then if valid pass on to the domain model.  I try to see my type 
constraints as a sort of "last chance" to catch problems before they generate a 
hard error in my database.

The only downside to my method is that you end up rewriting validation code a 
few times.  I know there are new tools cropping up to help with this, Reaction 
and Ernst from what I understand are attempts to let you infer your business 
validation needs from the type constraints on your storage object, but yet 
retain enough flexibility when your business validation needs different from 
the domain constraints.  I haven't had time to play with these yet, but maybe 
someone else on the list will jump in.

Until then I recommend having user input validation as a first layer before it 
tries to hit your Moose attributes.

my $0.02

John Napiorkowski


      

Reply via email to