1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
9 use Scalar::Util 'weaken';
13 # Set up the "usual" sqlite for DBICTest
14 my $schema = DBICTest->init_schema;
16 # This is how we're generating exceptions in the rest of these tests,
17 # which might need updating at some future time to be some other
18 # exception-generating statement:
20 my $throw = sub { $schema->resultset("Artist")->search(1,1,1) };
21 my $ex_regex = qr/Odd number of arguments to search/;
23 # Basic check, normal exception
24 throws_ok \&$throw, $ex_regex;
28 # Re-throw the exception with rethrow()
29 throws_ok { $e->rethrow }
31 isa_ok( $@, 'DBIx::Class::Exception' );
33 # Now lets rethrow via exception_action
35 my $handler_execution_counter = 0;
37 $schema->exception_action(sub {
38 $handler_execution_counter++;
39 like $_[0], $ex_regex, "Exception is precisely passed to exception_action";
43 throws_ok \&$throw, $ex_regex;
44 is $handler_execution_counter, 1, "exception_action handler executed exactly once";
48 # This should have never worked!!!
50 # Now lets suppress the error
51 $schema->exception_action(sub { 1 });
53 qr/exception_action handler .+ did \*not\* result in an exception.+original error: $ex_regex/;
55 # Now lets fall through and let croak take back over
56 $schema->exception_action(sub { return });
58 warnings_are \&$throw,
59 qr/exception_action handler installed .+ returned false instead throwing an exception/;
62 # again to see if no warning
64 warnings_are \&$throw,
69 # Whacky useless exception class
71 package DBICTest::Exception;
72 use overload '""' => \&stringify, fallback => 1;
75 bless { msg => shift }, $class;
79 die $self if ref $self eq __PACKAGE__;
80 die $self->new(shift);
83 "DBICTest::Exception is handling this: " . shift->{msg};
87 # Try the exception class
88 $schema->exception_action(sub { DBICTest::Exception->throw(@_) });
90 qr/DBICTest::Exception is handling this: $ex_regex/;
92 # While we're at it, lets throw a custom exception through Storage::DBI
93 throws_ok { $schema->storage->throw_exception('floob') }
94 qr/DBICTest::Exception is handling this: floob/;
98 DBICTest::AntiPattern::TrueZeroLen
99 DBICTest::AntiPattern::NullObject
102 my $exp_warn = qr/\QObjects of external exception class '$ap' stringify to '' (the empty string)/;
104 # make sure an exception_action can replace $@ with an antipattern
105 $schema->exception_action(sub { die $ap->new });
109 } $exp_warn, 'proper warning on antipattern encountered within exception_action';
111 # and make sure that the rethrow works
112 $schema->exception_action(sub { die @_ });
115 $schema->txn_do (sub { die $ap->new });
119 } $exp_warn, 'Proper warning on encountered antipattern';
122 # ensure we do not get into an infloop
124 weaken( my $s = $schema );
126 $schema->exception_action(sub{
127 $s->throw_exception(@_)
131 $schema->storage->dbh_do(sub {
132 $_[1]->do('wgwfwfwghawhjsejsethjwetjesjesjsejsetjes')