Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Custom error messages and moose

by neilwatson (Curate)
on Sep 29, 2012 at 00:39 UTC ( #996308=perlquestion: print w/ replies, xml ) Need Help??
neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, I have the following as part of a moose package.
has 'ip_address' => ( is => 'ro', isa => 'Str', lazy => 1, builder => 'get_ip_address', ... sub get_ip_address { my $self = shift; my $allclasses_file = $self->allclasses_file; my $ip_address; if ( $allclasses_file =~ m/_([^_]+$)/ ) { $ip_address = $1; return $ip_address; } }

This works. However, if the attribute is not correct the error messege is long and unhelpful. How can I make a custom more friendly messege when the ip_address attribute is incorrect?

Neil Watson
watson-wilson.ca

Comment on Custom error messages and moose
Download Code
Re: Custom error messages and moose
by irirgem (Novice) on Sep 29, 2012 at 04:04 UTC
    You could always put in a test like
    my $ip_address = ""; if ( $allclasses_file =~ m/_([^_]+$)/ ) { $ip_address = $1; unless ($ip_address =~ m/\d+\.\d+\.\d+\.\d+/) { print "Bad ip value $ip_address\n"; } } return $ip_address;
    or just change your code to use the '\d+\.\d+\.\d+\.\d+' like this
    my $ip_address = ""; if ( $allclasses_file =~ m/_(\d+\.\d+\.\d+\.\d+)$/) { $ip_address = $1; } return $ip_address;
Re: Custom error messages and moose
by remiah (Hermit) on Sep 29, 2012 at 11:57 UTC

    Do you show me your error message? if I return undef to isa 'Str' attribute, I will see like this.

    Attribute (ip_address) does not pass the type constraint because: Validation failed for 'Str' with value undef at reader test::ip_address (defined at 069.pl line 7) line 15
    	test::ip_address('test=HASH(0x284a2320)') called at 069.pl line 35
    
    

    Or, Is there some reason you can't die like this ?
    sub get_ip_address { my $self = shift; my $allclasses_file = $self->allclasses_file; my $ip_address; if ( $allclasses_file =~ m/_([^_]+$)/ ) { $ip_address = $1; return $ip_address; } else { die "not good ip\n"; } }
      There is pretty verbose:
      Use of uninitialized value $ip in gethostbyaddr at /home/neil/perl5/li +b/perl5/Evolvethinking/Unicorn.pm line 67, <FH> line 1. Attribute (fqhn) does not pass the type constraint because: Validation + failed for 'Str' with value undef at /home/neil/perl5/lib/perl5/x86_ +64-linux-thread-multi/Mouse/Util.pm line 383, <FH> line 1. Mouse::Util::throw_error('Mouse::Meta::Attribute=HASH(0x20342d +0)', 'Attribute (fqhn) does not pass the type constraint because: V.. +.', 'data', undef, 'depth', -1) called at /home/neil/perl5/lib/perl5/ +Evolvethinking/Unicorn.pm line 78 Unicorn::insert_record('Unicorn=HASH(0x20d3308)') called at /h +ome/neil/bin/p1.pl line 30

      Yes, I can put a die or warn in the sub but, I'm wondering if there is a more Moose like way.

      Neil Watson
      watson-wilson.ca

        If you set Environment variable MOOSE_ERROR_STYLE with

        export MOOSE_ERROR_STYLE=croak
        
        , you can cut confess style backtrace output.

        There seems to be discussion for error handling of Moose. I have no further experience for this. regards.

Re: Custom error messages and moose
by tobyink (Abbot) on Sep 29, 2012 at 15:00 UTC

    Type constraints allow custom error messages. So you could create an "IPAddress" type constraint as a subtype of "Str", and provide an error message for that.

    See Moose::Util::TypeConstraints.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996308]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-12-20 00:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (94 votes), past polls