1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
12 # Set up the "usual" sqlite for DBICTest
13 my $schema = DBICTest->init_schema;
15 # This is how we're generating exceptions in the rest of these tests,
16 # which might need updating at some future time to be some other
17 # exception-generating statement:
19 my $throw = sub { $schema->resultset("Artist")->search(1,1,1) };
20 my $ex_regex = qr/Odd number of arguments to search/;
22 # Basic check, normal exception
23 throws_ok \&$throw, $ex_regex;
27 # Re-throw the exception with rethrow()
28 throws_ok { $e->rethrow }
30 isa_ok( $@, 'DBIx::Class::Exception' );
32 # Now lets rethrow via exception_action
34 my $handler_execution_counter = 0;
36 $schema->exception_action(sub {
37 $handler_execution_counter++;
38 like $_[0], $ex_regex, "Exception is precisely passed to exception_action";
42 throws_ok \&$throw, $ex_regex;
43 is $handler_execution_counter, 1, "exception_action handler executed exactly once";
47 # This should have never worked!!!
49 # Now lets suppress the error
50 $schema->exception_action(sub { 1 });
52 qr/exception_action handler .+ did \*not\* result in an exception.+original error: $ex_regex/;
54 # Now lets fall through and let croak take back over
55 $schema->exception_action(sub { return });
57 warnings_are \&$throw,
58 qr/exception_action handler installed .+ returned false instead throwing an exception/;
61 # again to see if no warning
63 warnings_are \&$throw,
68 # Whacky useless exception class
70 package DBICTest::Exception;
71 use overload '""' => \&stringify, fallback => 1;
74 bless { msg => shift }, $class;
78 die $self if ref $self eq __PACKAGE__;
79 die $self->new(shift);
82 "DBICTest::Exception is handling this: " . shift->{msg};
86 # Try the exception class
87 $schema->exception_action(sub { DBICTest::Exception->throw(@_) });
89 qr/DBICTest::Exception is handling this: $ex_regex/;
91 # While we're at it, lets throw a custom exception through Storage::DBI
92 throws_ok { $schema->storage->throw_exception('floob') }
93 qr/DBICTest::Exception is handling this: floob/;
97 DBICTest::AntiPattern::TrueZeroLen
98 DBICTest::AntiPattern::NullObject
101 my $exp_warn = qr/\QObjects of external exception class '$ap' stringify to '' (the empty string)/;
103 # make sure an exception_action can replace $@ with an antipattern
104 $schema->exception_action(sub { die $ap->new });
108 } $exp_warn, 'proper warning on antipattern encountered within exception_action';
110 # and make sure that the rethrow works
111 $schema->exception_action(sub { die @_ });
114 $schema->txn_do (sub { die $ap->new });
118 } $exp_warn, 'Proper warning on encountered antipattern';