10 # Set up the "usual" sqlite for DBICTest
11 my $schema = DBICTest->init_schema;
13 # This is how we're generating exceptions in the rest of these tests,
14 # which might need updating at some future time to be some other
15 # exception-generating statement:
17 my $throw = sub { $schema->resultset("Artist")->search(1,1,1) };
18 my $ex_regex = qr/Odd number of arguments to search/;
20 # Basic check, normal exception
21 throws_ok \&$throw, $ex_regex;
25 # Re-throw the exception with rethrow()
26 throws_ok { $e->rethrow }
28 isa_ok( $@, 'DBIx::Class::Exception' );
30 # Now lets rethrow via exception_action
32 my $handler_execution_counter = 0;
34 $schema->exception_action(sub {
35 $handler_execution_counter++;
36 like $_[0], $ex_regex, "Exception is precisely passed to exception_action";
40 throws_ok \&$throw, $ex_regex;
41 is $handler_execution_counter, 1, "exception_action handler executed exactly once";
45 # This should have never worked!!!
47 # Now lets suppress the error
48 $schema->exception_action(sub { 1 });
50 qr/exception_action handler .+ did \*not\* result in an exception.+original error: $ex_regex/;
52 # Now lets fall through and let croak take back over
53 $schema->exception_action(sub { return });
55 warnings_are \&$throw,
56 qr/exception_action handler installed .+ returned false instead throwing an exception/;
59 # again to see if no warning
61 warnings_are \&$throw,
66 # Whacky useless exception class
68 package DBICTest::Exception;
69 use overload '""' => \&stringify, fallback => 1;
72 bless { msg => shift }, $class;
76 die $self if ref $self eq __PACKAGE__;
77 die $self->new(shift);
80 "DBICTest::Exception is handling this: " . shift->{msg};
84 # Try the exception class
85 $schema->exception_action(sub { DBICTest::Exception->throw(@_) });
87 qr/DBICTest::Exception is handling this: $ex_regex/;
89 # While we're at it, lets throw a custom exception through Storage::DBI
90 throws_ok { $schema->storage->throw_exception('floob') }
91 qr/DBICTest::Exception is handling this: floob/;
95 DBICTest::AntiPattern::TrueZeroLen
96 DBICTest::AntiPattern::NullObject
99 my $exp_warn = qr/\QObjects of external exception class '$ap' stringify to '' (the empty string)/;
101 # make sure an exception_action can replace $@ with an antipattern
102 $schema->exception_action(sub { die $ap->new });
106 } $exp_warn, 'proper warning on antipattern encountered within exception_action';
108 # and make sure that the rethrow works
109 $schema->exception_action(sub { die @_ });
112 $schema->txn_do (sub { die $ap->new });
116 } $exp_warn, 'Proper warning on encountered antipattern';