I'm going to build a request with HTTP::Request::Common and want to use its existing methods (POST, PUT, GET, etc.)
I'm curious how to control the error message better when coercing.
My class gets an HTTP method name as an attribute, and I wanted to validate it for how it will get used:
where { HTTP::Request::Common->can( $_ ) },
message { "The method provided [$_] does not appear to be a valid HTTP method" };
So, that generates a nice error message.
This is ugly, but to build the request I was then doing:
my $sub = HTTP::Request::Common->can( $self->http_method );
my $http_request = $sub->( $url, @args );
I thought I'd coerce the method name to that code reference and I would then use just:
my $http_request = $self->http_method->( $url, @args );
Not sure I have this correct, but the code to coerce is now:
subtype HttpRequestCode => as 'CodeRef';
where { HTTP::Request::Common->can( $_ ) },
message { "The method provided [$_] does not appear to be a valid HTTP method" };
coerce HttpRequestCode =>
from 'HttpMethod' => via { HTTP::Request::Common->can( $_ ) };
isa => 'HttpRequestCode',
Attribute (http_method) does not pass the type constraint because: Validation failed for 'HttpRequestCode' with value "PSOT"
but is there a way I can use my "message" while still coercing?
--
Bill Moseley
mos...@hank.org