Skip to content

Commit 33fdcd2

Browse files
committed
Add status_message_with_fallback GH#105,#114
1 parent 842cdf5 commit 33fdcd2

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

Changes

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Revision history for HTTP-Message
22

33
{{$NEXT}}
44
- Add support for RFC 8187 encoded filenames (GH#115) (Zaki Mughal)
5+
- Add status_message_with_fallback function that returns default status messages for unknown status codes. (GH#105,#114) (Robert Rothenberg)
56

67
6.18 2018-06-05 16:29:15Z
78
- Revert status_message to original code (GH#111) (Theo van Hoesel)

lib/HTTP/Status.pm

+25-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require 5.002; # because we use prototypes
99

1010
use base 'Exporter';
1111
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);
1313

1414
# Note also addition of mnemonics to @EXPORT below
1515

@@ -129,6 +129,18 @@ our %EXPORT_TAGS = (
129129

130130
sub status_message ($) { $StatusCode{$_[0]}; }
131131

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+
132144
sub is_info ($) { $_[0] && $_[0] >= 100 && $_[0] < 200; }
133145
sub is_success ($) { $_[0] && $_[0] >= 200 && $_[0] < 300; }
134146
sub is_redirect ($) { $_[0] && $_[0] >= 300 && $_[0] < 400; }
@@ -262,7 +274,18 @@ The status_message() function will translate status codes to human
262274
readable strings. The string is the same as found in the constant
263275
names above. If the $code is not registered in the L<list of IANA HTTP Status
264276
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.
266289
267290
=item is_info( $code )
268291

t/status-message-with-fallback.t

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use strict;
2+
use warnings;
3+
4+
use Test::More;
5+
plan tests => 12;
6+
7+
use HTTP::Status qw(status_message status_message_with_fallback);
8+
9+
foreach my $code (100, 200, 300, 400, 500) {
10+
is(status_message_with_fallback($code), status_message($code));
11+
}
12+
13+
is(status_message_with_fallback(0), undef);
14+
is(status_message_with_fallback(199), "OK");
15+
is(status_message_with_fallback(299), "OK");
16+
is(status_message_with_fallback(399), "Redirect");
17+
is(status_message_with_fallback(499), "Client Error");
18+
is(status_message_with_fallback(599), "Server Error");
19+
is(status_message_with_fallback(600), undef);

0 commit comments

Comments
 (0)