Skip to content

Commit

Permalink
Merge pull request jadeallenx#9 from miquelruiz/master
Browse files Browse the repository at this point in the history
Throw exception instead of return an error
  • Loading branch information
jadeallenx committed Jul 31, 2012
2 parents bac5bb2 + 3ccbd32 commit b82f379
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ requires 'URI' => 0;
requires 'Params::Validate' => 0;
requires 'XML::Simple' => 2.18;
requires 'Moose' => 0.38;
requires 'Carp' => 0;
build_requires 'Test::More' => 0;
build_requires 'Test::Exception' => 0;

no_index;

Expand Down
24 changes: 20 additions & 4 deletions lib/Net/Amazon/EC2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use MIME::Base64 qw(encode_base64 decode_base64);
use HTTP::Date qw(time2isoz);
use Params::Validate qw(validate SCALAR ARRAYREF HASHREF);
use Data::Dumper qw(Dumper);
use Carp;

use Net::Amazon::EC2::DescribeImagesResponse;
use Net::Amazon::EC2::DescribeKeyPairsResponse;
Expand Down Expand Up @@ -100,7 +101,7 @@ version of the EC2 API last updated January 1st, 2011.
my $result = $ec2->terminate_instances(InstanceId => $instance_id);
If an error occurs while communicating with EC2, the return value of these methods will be a Net::Amazon::EC2::Errors object.
If an error occurs while communicating with EC2, these methods will throw a L<Net::Amazon::EC2::Errors> exception.
=head1 DESCRIPTION
Expand Down Expand Up @@ -142,7 +143,11 @@ If set to a true value, the base_url will use https:// instead of http://. Setti
=item debug (optional)
A flag to turn on debugging. It is turned off by default
A flag to turn on debugging. Among other useful things, it will make the failing api calls print an stack trace. It is turned off by default
=item return_errors (optional)
By default, a failed api call will throw a L<Net::Amazon::EC2::Errors> object. Setting this option to a true value will make the failed calls return the object instead of throwing it. It is turned off by default.
=back
Expand All @@ -155,6 +160,7 @@ has 'signature_version' => ( is => 'ro', isa => 'Int', required => 1, default =>
has 'version' => ( is => 'ro', isa => 'Str', required => 1, default => '2011-01-01' );
has 'region' => ( is => 'ro', isa => 'Str', required => 1, default => 'us-east-1' );
has 'ssl' => ( is => 'ro', isa => 'Bool', required => 1, default => 0 );
has 'return_errors' => ( is => 'ro', isa => 'Bool', default => 0 );
has 'base_url' => (
is => 'ro',
isa => 'Str',
Expand Down Expand Up @@ -265,8 +271,18 @@ sub _parse_errors {
foreach my $error (@{$errors->errors}) {
$self->_debug("ERROR CODE: " . $error->code . " MESSAGE: " . $error->message . " FOR REQUEST: " . $errors->request_id);
}

return $errors;

# User wants old behaviour
if ($self->return_errors) {
return $errors;
}

# Print a stack trace if debugging is enabled
if ($self->debug) {
confess 'Last error was: ' . $es->[-1]->message;
} else {
croak $errors;
}
}

sub _debug {
Expand Down
46 changes: 46 additions & 0 deletions t/03_failing_calls.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use strict;
use blib;
use Test::More;
use Test::Exception;

BEGIN {
plan tests => 5;
use_ok( 'Net::Amazon::EC2' );
};

# Since you don't have an Amazon EC2 api endpoint running locally
# (you don't, right?) all api calls should fail, and thus allow us to
# test it properly.

my $access_id = 'xxx';
my $secret_key = 'yyy';
my $base_url = 'http://localhost';

my $die_ec2 = Net::Amazon::EC2->new(
AWSAccessKeyId => $access_id,
SecretAccessKey => $secret_key,
base_url => $base_url,
);

my $old_ec2 = Net::Amazon::EC2->new(
AWSAccessKeyId => $access_id,
SecretAccessKey => $secret_key,
base_url => $base_url,
return_errors => 1,
);

my $errors;
lives_ok { $errors = $old_ec2->describe_instances } "return_errors on , api call lives";
dies_ok { $die_ec2->describe_instances } "return_errors off, api call dies";

is_deeply ($@, $errors, "Same error thrown and returned");

my $dbg_ec2 = Net::Amazon::EC2->new(
AWSAccessKeyId => $access_id,
SecretAccessKey => $secret_key,
base_url => $base_url,
debug => 1,
);

dies_ok { $dbg_ec2->describe_instances } "with debug on also dies";

0 comments on commit b82f379

Please sign in to comment.