From: Jesse Luehrs Date: Thu, 5 May 2011 15:12:04 +0000 (-0500) Subject: docs about type constraint error messages X-Git-Tag: 2.0100~180 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3254961250f03954670a034f1a460db6e496e47a;p=gitmo%2FMoose.git docs about type constraint error messages --- diff --git a/lib/Moose/Manual/Types.pod b/lib/Moose/Manual/Types.pod index b4fda78..c314168 100644 --- a/lib/Moose/Manual/Types.pod +++ b/lib/Moose/Manual/Types.pod @@ -166,7 +166,9 @@ A subtype can also define its own constraint failure message. This lets you do things like have an error "The value you provided (20), was not a valid rating, which must be a number from 1-10." This is much friendlier than the default error, which just says that the value -failed a validation check for the type. +failed a validation check for the type. The default error can, however, +be made more friendly by installing L (version 0.14 or +higher), which Moose will use if possible to display the invalid value. Here's a simple (and useful) subtype example: diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 4600592..74d73ff 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -1042,6 +1042,22 @@ related C function. For a complete example see the F test file. +=head2 Error messages + +Type constraints can also specify custom error messages, for when they fail to +validate. This is provided as just another coderef, which receives the invalid +value in C<$_>, as in: + + subtype 'PositiveInt', + as 'Int', + where { $_ > 0 }, + message { "$_ is not a positive integer!" }; + +If no message is specified, a default message will be used, which indicates +which type constraint was being used and what value failed. If +L (version 0.14 or higher) is installed, it will be used to +display the invalid value, otherwise it will just be printed as is. + =head1 FUNCTIONS =head2 Type Constraint Constructors