Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / 34exception_action.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
4ffbc1d6 3use strict;
f54428ab 4use warnings;
4ffbc1d6 5
6use Test::More;
c3e9f718 7use Test::Exception;
8use Test::Warn;
7704dbc9 9use Scalar::Util 'weaken';
c0329273 10
4ffbc1d6 11use DBICTest;
12
4ffbc1d6 13# Set up the "usual" sqlite for DBICTest
14my $schema = DBICTest->init_schema;
15
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:
19
65d35121 20my $throw = sub { $schema->resultset("Artist")->search(1,1,1) };
4ffbc1d6 21my $ex_regex = qr/Odd number of arguments to search/;
22
23# Basic check, normal exception
65d35121 24throws_ok \&$throw, $ex_regex;
c3e9f718 25
26my $e = $@;
b2f408f3 27
28# Re-throw the exception with rethrow()
c3e9f718 29throws_ok { $e->rethrow }
30 $ex_regex;
b2f408f3 31isa_ok( $@, 'DBIx::Class::Exception' );
4ffbc1d6 32
33# Now lets rethrow via exception_action
4f29a592 34{
35 my $handler_execution_counter = 0;
36
37 $schema->exception_action(sub {
38 $handler_execution_counter++;
39 like $_[0], $ex_regex, "Exception is precisely passed to exception_action";
40 die @_
41 });
42
43 throws_ok \&$throw, $ex_regex;
44 is $handler_execution_counter, 1, "exception_action handler executed exactly once";
45}
4ffbc1d6 46
c3e9f718 47#
48# This should have never worked!!!
49#
4ffbc1d6 50# Now lets suppress the error
51$schema->exception_action(sub { 1 });
65d35121 52throws_ok \&$throw,
c3e9f718 53 qr/exception_action handler .+ did \*not\* result in an exception.+original error: $ex_regex/;
4ffbc1d6 54
55# Now lets fall through and let croak take back over
56$schema->exception_action(sub { return });
c3e9f718 57throws_ok {
65d35121 58 warnings_are \&$throw,
c3e9f718 59 qr/exception_action handler installed .+ returned false instead throwing an exception/;
60} $ex_regex;
61
62# again to see if no warning
63throws_ok {
65d35121 64 warnings_are \&$throw,
c3e9f718 65 [];
66} $ex_regex;
67
4ffbc1d6 68
69# Whacky useless exception class
70{
71 package DBICTest::Exception;
72 use overload '""' => \&stringify, fallback => 1;
73 sub new {
74 my $class = shift;
75 bless { msg => shift }, $class;
76 }
77 sub throw {
78 my $self = shift;
79 die $self if ref $self eq __PACKAGE__;
80 die $self->new(shift);
81 }
82 sub stringify {
83 "DBICTest::Exception is handling this: " . shift->{msg};
84 }
85}
86
87# Try the exception class
88$schema->exception_action(sub { DBICTest::Exception->throw(@_) });
65d35121 89throws_ok \&$throw,
c3e9f718 90 qr/DBICTest::Exception is handling this: $ex_regex/;
4ffbc1d6 91
92# While we're at it, lets throw a custom exception through Storage::DBI
c3e9f718 93throws_ok { $schema->storage->throw_exception('floob') }
94 qr/DBICTest::Exception is handling this: floob/;
9bf06dc0 95
84e4e006 96# test antipatterns
97for my $ap (qw(
98 DBICTest::AntiPattern::TrueZeroLen
99 DBICTest::AntiPattern::NullObject
100)) {
101 eval "require $ap";
102 my $exp_warn = qr/\QObjects of external exception class '$ap' stringify to '' (the empty string)/;
103
104 # make sure an exception_action can replace $@ with an antipattern
105 $schema->exception_action(sub { die $ap->new });
90a5b023 106 warnings_exist {
84e4e006 107 eval { $throw->() };
108 isa_ok $@, $ap;
109 } $exp_warn, 'proper warning on antipattern encountered within exception_action';
110
ddcc02d1 111 # and make sure that the rethrow works
84e4e006 112 $schema->exception_action(sub { die @_ });
90a5b023 113 warnings_exist {
84e4e006 114 eval {
115 $schema->txn_do (sub { die $ap->new });
116 };
117
118 isa_ok $@, $ap;
119 } $exp_warn, 'Proper warning on encountered antipattern';
120}
121
7704dbc9 122# ensure we do not get into an infloop
123{
124 weaken( my $s = $schema );
125
126 $schema->exception_action(sub{
127 $s->throw_exception(@_)
128 });
129
130 throws_ok {
131 $schema->storage->dbh_do(sub {
132 $_[1]->do('wgwfwfwghawhjsejsethjwetjesjesjsejsetjes')
133 } )
134 } qr/syntax error/i;
135}
136
68fe9141 137done_testing;