Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/Net/ACME2/HTTP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ sub _xform_http_error {
%{ JSON::decode_json( $exc->get('content') ) },
);
};
my $json_parse_err = $@;

if ($acme_error) {
die Net::ACME2::X->create(
Expand All @@ -245,6 +246,16 @@ sub _xform_http_error {
},
);
}

if ($json_parse_err) {
my $content = $exc->get('content');

die Net::ACME2::X->create(
'Generic',
"Failed to decode ACME error ($json_parse_err); HTTP status ${\$exc->get('status')}: $content",
{ http => $exc },
);
}
}

die $exc;
Expand Down
121 changes: 119 additions & 2 deletions t/Net-ACME2-HTTP.t
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,14 @@ my $acme_key = Net::ACME2::AccountKey->new($_KEY_PEM);

ok($err, '_xform_http_error re-throws when DESTROY clobbers $@');
ok(
eval { $err->isa('Net::ACME2::X::HTTP::Protocol') },
'_xform_http_error preserves exception type (not empty string)',
eval { $err->isa('Net::ACME2::X::Generic') },
'_xform_http_error wraps unparsable body in Generic exception',
) or diag("Got: " . (defined $err ? $err : "(undef)"));

# The original HTTP::Protocol exception is accessible via get('http')
ok(
eval { $err->get('http')->isa('Net::ACME2::X::HTTP::Protocol') },
'_xform_http_error carries original HTTP::Protocol in "http" property',
) or diag("Got: " . (defined $err ? $err : "(undef)"));
}

Expand Down Expand Up @@ -732,4 +738,115 @@ my $acme_key = Net::ACME2::AccountKey->new($_KEY_PEM);
) or diag("Got: " . (defined $err ? $err : "(undef)"));
}

#----------------------------------------------------------------------
# Test: non-JSON error body includes parse failure in exception
#
# When a server returns a non-JSON error body (e.g., an HTML proxy error),
# JSON::decode_json() fails inside _xform_http_error(). The parse failure
# and raw response content must be surfaced in the exception so the caller
# can diagnose the server problem — not silently swallowed.
#----------------------------------------------------------------------

{
my $mock = MockUA->new(responses => []);

no warnings 'redefine';
local *MockUA::request = sub {
my ($self, $method, $url, $args) = @_;

die Net::ACME2::X->create(
'HTTP::Protocol',
{
method => 'GET',
url => $url,
status => 502,
reason => 'Bad Gateway',
headers => {},
content => '<html><body>Bad Gateway</body></html>',
},
);
};
use warnings 'redefine';

my $http = Net::ACME2::HTTP->new(
key => $acme_key,
ua => $mock,
);

my $err;
eval {
$http->get('https://example.com/directory');
1;
} or $err = $@;

ok($err, 'non-JSON error body throws exception');

my $err_str = "$err";

like(
$err_str,
qr/Bad Gateway/,
'exception includes raw response content',
);

like(
$err_str,
qr/JSON|decode|parse/i,
'exception mentions JSON parse failure',
);
}

#----------------------------------------------------------------------
# Test: truncated JSON error body includes parse failure in exception
#----------------------------------------------------------------------

{
my $mock = MockUA->new(responses => []);

no warnings 'redefine';
local *MockUA::request = sub {
my ($self, $method, $url, $args) = @_;

die Net::ACME2::X->create(
'HTTP::Protocol',
{
method => 'GET',
url => $url,
status => 500,
reason => 'Internal Server Error',
headers => {},
content => '{"type":"urn:ietf:params:acme:error:serverInternal","deta',
},
);
};
use warnings 'redefine';

my $http = Net::ACME2::HTTP->new(
key => $acme_key,
ua => $mock,
);

my $err;
eval {
$http->get('https://example.com/directory');
1;
} or $err = $@;

ok($err, 'truncated JSON error body throws exception');

my $err_str = "$err";

like(
$err_str,
qr/serverInternal/,
'truncated JSON: exception includes partial response content',
);

# Verify the original HTTP exception is accessible
ok(
eval { $err->get('http')->isa('Net::ACME2::X::HTTP::Protocol') },
'exception carries original HTTP::Protocol error',
);
}

done_testing();
Loading