@@ -9,7 +9,7 @@ require 5.002; # because we use prototypes
9
9
10
10
use base ' Exporter' ;
11
11
our @EXPORT = qw( is_info is_success is_redirect is_error status_message) ;
12
- our @EXPORT_OK = qw( is_client_error is_server_error is_cacheable_by_default) ;
12
+ our @EXPORT_OK = qw( is_client_error is_server_error is_cacheable_by_default status_message_with_fallback ) ;
13
13
14
14
# Note also addition of mnemonics to @EXPORT below
15
15
@@ -129,6 +129,18 @@ our %EXPORT_TAGS = (
129
129
130
130
sub status_message ($) { $StatusCode {$_ [0]}; }
131
131
132
+ sub status_message_with_fallback ($) {
133
+ status_message( $_ [0] )
134
+ || (
135
+ is_info( $_ [0] ) ? ' OK'
136
+ : is_success( $_ [0] ) ? ' OK'
137
+ : is_redirect( $_ [0] ) ? ' Redirect'
138
+ : is_client_error( $_ [0] ) ? ' Client Error'
139
+ : is_server_error( $_ [0] ) ? ' Server Error'
140
+ : undef
141
+ );
142
+ }
143
+
132
144
sub is_info ($) { $_ [0] && $_ [0] >= 100 && $_ [0] < 200; }
133
145
sub is_success ($) { $_ [0] && $_ [0] >= 200 && $_ [0] < 300; }
134
146
sub is_redirect ($) { $_ [0] && $_ [0] >= 300 && $_ [0] < 400; }
@@ -262,7 +274,18 @@ The status_message() function will translate status codes to human
262
274
readable strings. The string is the same as found in the constant
263
275
names above. If the $code is not registered in the L<list of IANA HTTP Status
264
276
Codes|https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml>
265
- then C<undef > is returned.
277
+ then C<undef > is returned.
278
+
279
+ =item status_message_with_fallback( $code )
280
+
281
+ This function will return corresponding status message, if the code is
282
+ defined. Otherwise it will return a default message based on the
283
+ code range.
284
+
285
+ Use this function instead of C<status_message > if your code always
286
+ assumes that there is a defined status message.
287
+
288
+ This function is not exported by default.
266
289
267
290
=item is_info( $code )
268
291
0 commit comments