Remove duplicate test (same as end of t/storage/error.t)
[dbsrgits/DBIx-Class.git] / t / 34exception_action.t
CommitLineData
4ffbc1d6 1use strict;
f54428ab 2use warnings;
4ffbc1d6 3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
4ffbc1d6 8# Set up the "usual" sqlite for DBICTest
9my $schema = DBICTest->init_schema;
10
11# This is how we're generating exceptions in the rest of these tests,
12# which might need updating at some future time to be some other
13# exception-generating statement:
14
15sub throwex { $schema->resultset("Artist")->search(1,1,1); }
16my $ex_regex = qr/Odd number of arguments to search/;
17
18# Basic check, normal exception
19eval { throwex };
b2f408f3 20my $e = $@; # like() seems to stringify $@
21like($@, $ex_regex);
22
23# Re-throw the exception with rethrow()
24eval { $e->rethrow };
25isa_ok( $@, 'DBIx::Class::Exception' );
4ffbc1d6 26like($@, $ex_regex);
27
28# Now lets rethrow via exception_action
29$schema->exception_action(sub { die @_ });
30eval { throwex };
31like($@, $ex_regex);
32
33# Now lets suppress the error
34$schema->exception_action(sub { 1 });
35eval { throwex };
36ok(!$@, "Suppress exception");
37
38# Now lets fall through and let croak take back over
39$schema->exception_action(sub { return });
40eval { throwex };
41like($@, $ex_regex);
42
43# Whacky useless exception class
44{
45 package DBICTest::Exception;
46 use overload '""' => \&stringify, fallback => 1;
47 sub new {
48 my $class = shift;
49 bless { msg => shift }, $class;
50 }
51 sub throw {
52 my $self = shift;
53 die $self if ref $self eq __PACKAGE__;
54 die $self->new(shift);
55 }
56 sub stringify {
57 "DBICTest::Exception is handling this: " . shift->{msg};
58 }
59}
60
61# Try the exception class
62$schema->exception_action(sub { DBICTest::Exception->throw(@_) });
63eval { throwex };
64like($@, qr/DBICTest::Exception is handling this: $ex_regex/);
65
66# While we're at it, lets throw a custom exception through Storage::DBI
c216324a 67eval { $schema->storage->throw_exception('floob') };
4ffbc1d6 68like($@, qr/DBICTest::Exception is handling this: floob/);
9bf06dc0 69
70
68fe9141 71done_testing;