File Coverage

blib/lib/Rose/HTML/Object/Errors.pm
Criterion Covered Total %
statement 111 111 100.0
branch n/a
condition n/a
subroutine 32 32 100.0
pod 0 1 0.0
total 143 144 99.3


line stmt bran cond sub pod time code
1             package Rose::HTML::Object::Errors;
2              
3 45     45   429039 use strict;
  45         156  
  45         1399  
4              
5 45     45   246 use Carp;
  45         102  
  45         2553  
6              
7 45     45   267 use base 'Rose::HTML::Object::Messages';
  45         148  
  45         21747  
8              
9             our $VERSION = '0.600';
10              
11             BEGIN
12             {
13 45     45   512 __PACKAGE__->message_names([]);
14 45         492 __PACKAGE__->message_id_to_name_map({});
15 45         402 __PACKAGE__->message_name_to_id_map({});
16             }
17              
18             BEGIN
19             {
20 45     45   6377 *error_ids = \&Rose::HTML::Object::Messages::message_ids;
21 45         133 *error_id_exists = \&Rose::HTML::Object::Messages::message_id_exists;
22 45         104 *error_name_exists = \&Rose::HTML::Object::Messages::message_name_exists;
23              
24 45         125 *get_error_id = \&Rose::HTML::Object::Messages::get_message_id;
25 45         107 *get_error_name = \&Rose::HTML::Object::Messages::get_message_name;
26              
27 45         95 *add_error = \&Rose::HTML::Object::Messages::add_message;
28 45         96 *add_errors = \&Rose::HTML::Object::Messages::add_messages;
29 45         99 *get_error_id = \&Rose::HTML::Object::Messages::get_message_id;
30              
31 45         98 *add_error = \&Rose::HTML::Object::Messages::add_message;
32 45         94 *add_errors = \&Rose::HTML::Object::Messages::add_messages;
33              
34 45         12734 *error_names = \&Rose::HTML::Object::Messages::message_names;
35             }
36              
37             sub init_export_tags
38             {
39 193     193 0 436 my($class) = shift;
40              
41 193         727 my $list = $class->error_names;
42              
43             $class->export_tags
44             (
45             all => $list,
46 4847         9792 field => [ grep { /^FIELD_/ } @$list ],
47 4847         8431 form => [ grep { /^FORM_/ } @$list ],
48 4847         8321 date => [ grep { /^DATE_/ } @$list ],
49 4847         8552 time => [ grep { /^TIME_/ } @$list ],
50 4847         8151 email => [ grep { /^EMAIL_/ } @$list ],
51 4847         8284 phone => [ grep { /^PHONE_/ } @$list ],
52 4847         8863 number => [ grep { /^NUM_/ } @$list ],
53 4847         8223 set => [ grep { /^SET_/ } @$list ],
54 193         2292 string => [ grep { /^STRING_/ } @$list ],
  4847         8911  
55             );
56             }
57              
58             #
59             # Errors
60             #
61              
62 45     45   363 use constant CUSTOM_ERROR => -1;
  45         108  
  45         3100  
63              
64             # Field errors
65 45     45   334 use constant FIELD_REQUIRED => 3;
  45         112  
  45         2480  
66 45     45   309 use constant FIELD_PARTIAL_VALUE => 8;
  45         127  
  45         2601  
67 45     45   291 use constant FIELD_INVALID => 9;
  45         88  
  45         2557  
68              
69             # Form errors
70 45     45   292 use constant FORM_HAS_ERRORS => 100;
  45         107  
  45         2267  
71              
72             # Numerical errors
73 45     45   291 use constant NUM_INVALID_INTEGER => 1300;
  45         129  
  45         2498  
74 45     45   292 use constant NUM_INVALID_INTEGER_POSITIVE => 1301;
  45         138  
  45         2308  
75 45     45   287 use constant NUM_NOT_POSITIVE_INTEGER => 1302;
  45         90  
  45         2389  
76 45     45   293 use constant NUM_BELOW_MIN => 1303;
  45         92  
  45         2270  
77 45     45   289 use constant NUM_ABOVE_MAX => 1304;
  45         120  
  45         2212  
78 45     45   312 use constant NUM_INVALID_NUMBER => 1305;
  45         128  
  45         2359  
79 45     45   293 use constant NUM_INVALID_NUMBER_POSITIVE => 1306;
  45         101  
  45         2746  
80 45     45   332 use constant NUM_NOT_POSITIVE_NUMBER => 1307;
  45         109  
  45         3378  
81              
82              
83             # String errors
84 45     45   303 use constant STRING_OVERFLOW => 1400;
  45         150  
  45         2588  
85              
86             # Date errors
87 45     45   329 use constant DATE_INVALID => 1500;
  45         111  
  45         2364  
88 45     45   290 use constant DATE_MIN_GREATER_THAN_MAX => 1501;
  45         117  
  45         2263  
89              
90             # Time errors
91 45     45   301 use constant TIME_INVALID => 1550;
  45         129  
  45         2285  
92 45     45   323 use constant TIME_INVALID_HOUR => 1551;
  45         109  
  45         2267  
93 45     45   292 use constant TIME_INVALID_MINUTE => 1552;
  45         125  
  45         2437  
94 45     45   307 use constant TIME_INVALID_SECONDS => 1553;
  45         126  
  45         2330  
95 45     45   295 use constant TIME_INVALID_AMPM => 1554;
  45         102  
  45         2410  
96              
97             # Email errors
98 45     45   309 use constant EMAIL_INVALID => 1600;
  45         122  
  45         2651  
99              
100             # Phone errors
101 45     45   302 use constant PHONE_INVALID => 1650;
  45         116  
  45         2676  
102              
103             # Set errors
104 45     45   372 use constant SET_INVALID_QUOTED_STRING => 1700;
  45         88  
  45         2372  
105 45     45   314 use constant SET_PARSE_ERROR => 1701;
  45         97  
  45         2763  
106              
107 45     45   284 BEGIN { __PACKAGE__->add_errors }
108              
109             1;
110              
111             __END__
112              
113             =head1 NAME
114              
115             Rose::HTML::Object::Errors - Error ids and named constants for use with HTML objects.
116              
117             =head1 SYNOPSIS
118              
119             package My::HTML::Object::Errors;
120              
121             use strict;
122              
123             # Import the standard set of error ids
124             use Rose::HTML::Object::Errors qw(:all);
125             use base qw(Rose::HTML::Object::Errors);
126              
127             ##
128             ## Define your new error ids below
129             ##
130              
131             # Error ids from 0 to 29,999 are reserved for built-in errors.
132             # Negative error ids are reserved for internal use. Please use error
133             # ids 30,000 or higher for your errors. Suggested error id ranges
134             # and naming conventions for various error types are shown below.
135              
136             # Field errors
137              
138             use constant FIELD_ERROR_PASSWORD_TOO_SHORT => 101_000;
139             use constant FIELD_ERROR_USERNAME_INVALID => 101_001;
140             ...
141              
142             # Generic errors
143              
144             use constant LOGIN_NO_SUCH_USER => 200_000;
145             use constant LOGIN_USER_EXISTS_ERROR => 200_001;
146             ...
147              
148             # This line must be below all the "use constant ..." declarations
149             BEGIN { __PACKAGE__->add_errors }
150              
151             1;
152              
153             =head1 DESCRIPTION
154              
155             L<Rose::HTML::Object::Errors> stores error ids and names. The error ids are defined as Perl L<constants|constant> with integer values. The constants themselves as well as the mapping between the symbolic constant names and their values are stored as class data.
156              
157             If you merely want to import one of the standard error id constants, you may use this module as-is (see the L<EXPORTS|/EXPORTS> section for details). If you want to define your own errors, you must subclass this module exactly as shown in the synopsis. The order of the statements is important!
158              
159             When adding your own errors, you are free to choose any integer error id values, subject to the following constraints.
160              
161             =over 4
162              
163             =item * Error ids from 0 to 29,999 are reserved for built-in errors.
164              
165             =item * Negative error ids are reserved for internal use.
166              
167             =back
168              
169             Please use ids 30,000 or higher for your errors. Constant names may contain only the characters C<[A-Z0-9_]> and must be unique among all error constant names.
170              
171             =head1 EXPORTS
172              
173             L<Rose::HTML::Object::Errors> does not export any symbols by default.
174              
175             The 'all' tag:
176              
177             use Rose::HTML::Object::Errors qw(:all);
178              
179             will cause all error name constant to be imported.
180              
181             The following tags will cause all errors whose names match the regular expression to the right of the tag name to be imported.
182              
183             TAG NAME REGEX
184             ----- ----------
185             field ^FIELD_
186             form ^FORM_
187             date ^DATE_
188             time ^TIME_
189             email ^EMAIL_
190             phone ^PHONE_
191             number ^NUM_
192             set ^SET_
193             string ^STRING_
194              
195             For example, this will import all the error constants whose names begin with "FIELD_"
196              
197             use Rose::HTML::Object::Errors qw(:field);
198              
199             Finally, you can import individual error constant names as well:
200              
201             use Rose::HTML::Object::Errors qw(FIELD_REQUIRED NUM_INVALID_INTEGER);
202              
203             A complete listing of the default set of error constant names appears in the next section.
204              
205             =head1 BUILT-IN ERRORS
206              
207             The list of built-in errors appears below. You should not rely on the actual numeric values of these constants. Import and refer to them only by their symbolic names.
208              
209             FIELD_REQUIRED
210             FIELD_PARTIAL_VALUE
211             FIELD_INVALID
212              
213             FORM_HAS_ERRORS
214              
215             NUM_INVALID_INTEGER
216             NUM_INVALID_INTEGER_POSITIVE
217             NUM_NOT_POSITIVE_INTEGER
218             NUM_BELOW_MIN
219             NUM_ABOVE_MAX
220             NUM_INVALID_NUMBER
221             NUM_INVALID_NUMBER_POSITIVE
222             NUM_NOT_POSITIVE_NUMBER
223              
224             STRING_OVERFLOW
225              
226             DATE_INVALID
227             DATE_MIN_GREATER_THAN_MAX
228              
229             TIME_INVALID
230             TIME_INVALID_HOUR
231             TIME_INVALID_MINUTE
232             TIME_INVALID_SECONDS
233             TIME_INVALID_AMPM
234              
235             EMAIL_INVALID
236              
237             PHONE_INVALID
238              
239             SET_INVALID_QUOTED_STRING
240             SET_PARSE_ERROR
241              
242             =head1 CLASS METHODS
243              
244             =over 4
245              
246             =item B<add_error NAME, ID>
247              
248             Add a new error constant with NAME and an integer ID value. Error ids from 0 to 29,999 are reserved for built-in errors. Negative error ids are reserved for internal use. Please use error ids 30,000 or higher for your errors. Constant names may contain only the characters C<[A-Z0-9_]> and must be unique among all error names.
249              
250             =item B<add_errors [NAME1, NAME2, ...]>
251              
252             If called with no arguments, this method L<adds|/add_error> all error L<constants|constant> defined in the calling class. Example:
253              
254             __PACKAGE__->add_errors;
255              
256             If called with a list of constant names, add each named constant to the list of errors. These L<constants|constant> must already exist in the calling class. Example:
257              
258             use constant MY_ERROR1 => 123456;
259             use constant MY_ERROR2 => 123457;
260             ...
261             __PACKAGE__->add_errors('MY_ERROR1', 'MY_ERROR2');
262              
263             =item B<get_error_id NAME>
264              
265             Returns the integer error id corresponding to the symbolic constant NAME, or undef if no such name exists.
266              
267             =item B<get_error_name ID>
268              
269             Returns the symbolic error constant name corresponding to the integer error ID, or undef if no such error ID exists.
270              
271             =item B<error_id_exists ID>
272              
273             Return true of the integer error ID exists, false otherwise.
274              
275             =item B<error_name_exists NAME>
276              
277             Return true of the symbolic error constant NAME exists, false otherwise.
278              
279             =item B<error_ids>
280              
281             Returns a list (in list context) or reference to an array (in scalar context) of integer error ids.
282              
283             =item B<error_names>
284              
285             Returns a list (in list context) or reference to an array (in scalar context) of error names.
286              
287             =back
288              
289             =head1 AUTHOR
290              
291             John C. Siracusa (siracusa@gmail.com)
292              
293             =head1 LICENSE
294              
295             Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.