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