Permalink
Browse files

Finished with Exceptions

Exceptions are finished, some tests are in, I'm generally happy with the
product. Tomorrow is the ModExec base class, I think.
  • Loading branch information...
manchicken committed Jan 29, 2014
1 parent 91520f3 commit 2b6a38e1a8c921a860ba742f01cf0663e4ffc2f3
Showing with 60 additions and 15 deletions.
  1. +1 −0 .gitignore
  2. +23 −6 lib/ModExec/Exception.pm
  3. +36 −9 t/exceptions.t
@@ -0,0 +1 @@
commit-template
@@ -9,6 +9,7 @@ package ModExec::Exception;
use strict;
use warnings;
use 5.010;
use base qw/Exporter/;

=head1 VERSION
@@ -22,19 +23,19 @@ our $VERSION = '1.0';
=over 4
=item Error::Simple (base class)
=item Error (for error handling and exceptions)
=item Try::Tiny (for syntax sugar)
=item Carp (stack traces, uses longmess)
=back
=cut

use base 'Error::Simple';
use Carp;
use Error qw/:try/;
use Try::Tiny;

# No Critic DGR: Makes life easier for our users
our @EXPORT = qw/try catch finally/; ## no critic (ProhibitAutomaticExportation)

=head1 SYNOPSIS
@@ -69,7 +70,7 @@ Returns an instance of the exception.

sub new {
my ( $pkg, $error_code, $error_message ) = @_;
my $self = $pkg->SUPER::new($error_message);
my $self = {};

bless $self, $pkg;

@@ -158,6 +159,22 @@ sub stringify {
return $self->errcode() . ": " . $self->errstr() . "\n" . $self->stack();
}

=head2 throw()
Die with an instance of this class.
Usage: throw ModExec::Exception->new( $code, $message )
=cut

sub throw {
my ($pkg, @args) = @_;

# No Critic DGR: Carp::croak screws up the object being thrown.
die $pkg->new( @args ); ## no critic (RequireCarping)
}


1;

=head1 DIAGNOSTICS
@@ -18,30 +18,57 @@ use strict;
use warnings;
use 5.010;

use Test::More tests => 7;
use Test::More tests => 9;
use Test::Exception;
use English q{-no_match_vars};
use Data::Dumper;

# Load the module using C<use_ok()>
use_ok q{ModExec::Exception};
sub BEGIN {
use_ok q{ModExec::Exception};
}

# Verify that instantiation gives us both a C<ModExec::Exception> and C<Error::Simple>
# Verify that instantiation gives us a C<ModExec::Exception> instance.
my $test1 = ModExec::Exception->new();
isa_ok $test1, q{ModExec::Exception}, 'Verify we got a ModExec::Exception instance...';
isa_ok $test1, q{Error::Simple}, 'Verify we got an Error::Simple instance...';
isa_ok $test1, q{ModExec::Exception},
'Verify we got a ModExec::Exception instance...';

# Verify we can set just the error message
$test1->errstr( q{TEST1} );
$test1->errstr(q{TEST1});
is $test1->errstr(), q{TEST1}, 'Verify we can set only the error message...';

# Verify we can set just the error code
$test1->errcode( q{ERR_CODE1} );
is $test1->errcode(), q{ERR_CODE1}, 'Verify we can set only the error code...';
$test1->errcode(q{ERR_CODE1});
is $test1->errcode(), q{ERR_CODE1},
'Verify we can set only the error code...';

# Verify we can set both error code and error message via errstr()...
$test1->errstr( q{TEST2}, q{ERR_CODE2} );
is $test1->errstr(), q{TEST2}, 'Verify we set the error message...';
is $test1->errstr(), q{TEST2}, 'Verify we set the error message...';
is $test1->errcode(), q{ERR_CODE2}, 'Verify we set the error code...';

# Verify that we have a stack which includes our test name...
like $test1->stack(), qr{$0}x,
'Verify that the stack trace includes the test script name...';

# Verify stringify
like $test1->stringify(), qr{
\A # The beginning of the string
ERR_CODE2 # The error code we last set
:\s # A colon and a space
TEST2 # The string TEST2 for the error message
\n # A literal newline
.*?$0 # The program name
}x, 'Verify that our stringify output looks correct...';

# Let's verify that this behaves like an exception with Error qw/:try/ sugar
my $boom = 0;
try {
ModExec::Exception->throw( 'ERR_CODE3', 'TEST3' );
} catch {
$boom = $ARG;
};
isa_ok $boom, q{ModExec::Exception}, 'Verify that Error\'s try/catch sugar works...';

=head1 SEE ALSO

0 comments on commit 2b6a38e

Please sign in to comment.