Skip Menu |

This queue is for tickets about the Data-Domain CPAN distribution.

Report information
The Basics
Id: 106003
Status: resolved
Priority: 0/
Queue: Data-Domain

People
Owner: Nobody in particular
Requestors: djerius [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Dynamic error messages
I'm trying to create a new domain to validate paths, using Path::Tiny. Path::Tiny throws exceptions upon some errors, and I'd like to pass the text on to the caller. There doesn't seem to be a way of doing that outside of something really nasty like this: sub _inspect { my ( $self, $path ) = @_; my $obj = eval {path( $path );}; if ( $@ ) { $self->{-messages}{INVALID} //= "%s: $@"; return $self->msg( INVALID => $path ); } return; } Is there some cleaner way of doing this? Thanks, Diab
On Tue Jul 21 16:09:38 2015, DJERIUS wrote: Show quoted text
> I'm trying to create a new domain to validate paths, using Path::Tiny. > Path::Tiny throws exceptions upon some errors, and I'd like to pass > the text on to the caller. There doesn't seem to be a way of doing > that outside of something really nasty like this: > > sub _inspect { > > my ( $self, $path ) = @_; > > my $obj = eval {path( $path );}; > > if ( $@ ) { > $self->{-messages}{INVALID} //= "%s: $@"; > return $self->msg( INVALID => $path ); > } > > return; > } > > > Is there some cleaner way of doing this? > > Thanks, > > Diab
Aha; I see that the msgs method accepts any number of arguments, so I could do this in the constructor: $self->{-message}{INVALID} = 'unable to parse "%s" as a path: %s'; and in the _inspect routine: return $self->msg( INVALID => $path, $err ); It would be nice if something like String::Errf were used under the hood, so the order of arguments wouldn't matter. Thanks, Diab
Subject: Re: [rt.cpan.org #106003] Dynamic error messages
Date: Tue, 21 Jul 2015 22:45:27 +0200
To: bug-Data-Domain [...] rt.cpan.org
From: Laurent Dami <laurent.dami [...] free.fr>
Le 21.07.2015 22:09, Diab Jerius via RT a écrit : Show quoted text
> Tue Jul 21 16:09:38 2015: Request 106003 was acted upon. > Transaction: Ticket created by DJERIUS > Queue: Data-Domain > Subject: Dynamic error messages > Broken in: (no value) > Severity: (no value) > Owner: Nobody > Requestors: djerius@cpan.org > Status: new > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=106003 > > > > I'm trying to create a new domain to validate paths, using Path::Tiny. Path::Tiny throws exceptions upon some errors, and I'd like to pass the text on to the caller. There doesn't seem to be a way of doing that outside of something really nasty like this: > > sub _inspect { > > my ( $self, $path ) = @_; > > my $obj = eval {path( $path );}; > > if ( $@ ) { > $self->{-messages}{INVALID} //= "%s: $@"; > return $self->msg( INVALID => $path ); > } > > return; > } > > > Is there some cleaner way of doing this? > > Thanks, > > Diab >
Hi, Your domain does not necessarily need to implement the -messages API. So the simplest thing to do is just to return a string containing the error. sub _inspect { my ( $self, $path ) = @_; my $obj = eval {path( $path );}; my $err = $@; return $err; # if undef, the inspection was } Now if you want to implement the -messages API, be careful to fully understand how it works. The idea is that domain signals typed errors, but the client of the domain decides about the error messages. So the domain implementation should not modify $self->{messages}, this breaks the contract. The domain should just return $self->msg(INVALID => ...), and the client of the domain should specify (at domain creation time) which string is associated to INVALID. Regarding the order of arguments : look at the sprintf documentation, there is syntax to refer to arguments by position, bypassing the natural order. Good luck, Laurent Dami
On Tue Jul 21 16:45:50 2015, laurent.dami@free.fr wrote: Show quoted text
> Le 21.07.2015 22:09, Diab Jerius via RT a écrit :
> > Tue Jul 21 16:09:38 2015: Request 106003 was acted upon. > > Transaction: Ticket created by DJERIUS > > Queue: Data-Domain > > Subject: Dynamic error messages > > Broken in: (no value) > > Severity: (no value) > > Owner: Nobody > > Requestors: djerius@cpan.org > > Status: new > > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=106003 > > > > > > > I'm trying to create a new domain to validate paths, using > > Path::Tiny. Path::Tiny throws exceptions upon some errors, and I'd > > like to pass the text on to the caller. There doesn't seem to be a > > way of doing that outside of something really nasty like this: > >
[...] Show quoted text
> Hi, > > Your domain does not necessarily need to implement the -messages API. > So the simplest thing to do is just to return a string containing the > error. >
Thanks; I missed the forest for the trees there. Show quoted text
> Now if you want to implement the -messages API, be careful to fully > understand how it works. The idea is that domain signals typed > errors, but the client of the domain decides about the error > messages. So the domain implementation should not modify > $self->{messages}, this breaks the contract. The domain should just > return $self->msg(INVALID => ...), and the client of the domain > should specify (at domain creation time) which string is associated > to INVALID.
The current design has a single domain (Path), with options selecting which tests to perform on the argument (i.e. -is_dir, -is_file, etc.). Returning a single INVALID tag won't provide specific enough information. Would this be an acceptable approach? my %messages = ( NOT_IS_DIR => '%s is not a directory', NOT_IS_FILE => '%s is not a file', NOT_EXISTS => '%s does not exist', NOT_IS_ABSOLUTE => '%s is not absolute', NOT_IS_RELATIVE => '%s is not relative', NOT_IS_ROOTDIR => '%s is not a root directory' ); my @tests = qw/ exists is_file is_dir is_absolute is_relative is_rootdir /; sub new { my $class = shift; my $self = bless Data::Domain::_parse_args( \@_, [ map { '-' . $_ } @tests ] ), $class; $self->{-messages} //= \%messages; return $self; } Show quoted text
> Regarding the order of arguments : look at the sprintf documentation, > there is syntax to refer to arguments by position, bypassing the > natural > order.
Thanks. That'll do it (not quite as user-friendly as passing in a hash, though). Thanks, Diab
Subject: Re: [rt.cpan.org #106003] Dynamic error messages
Date: Wed, 22 Jul 2015 21:07:13 +0200
To: bug-Data-Domain [...] rt.cpan.org
From: Laurent Dami <laurent.dami [...] free.fr>
Le 22.07.2015 19:53, Diab Jerius via RT a écrit : Show quoted text
> Would this be an acceptable approach?
Yes, this makes sense because the call to the ->new() method can still override the -messages option, and in case there is none, the constructor points to default messages.
On Wed Jul 22 15:07:30 2015, laurent.dami@free.fr wrote: Show quoted text
> Le 22.07.2015 19:53, Diab Jerius via RT a écrit :
> > Would this be an acceptable approach?
> Yes, this makes sense because the call to the ->new() method can still > override the -messages option, and in case there is none, the > constructor points to default messages. >
Thanks for the feedback.