Commit | Line | Data |
4ffbc1d6 |
1 | use strict; |
f54428ab |
2 | use warnings; |
4ffbc1d6 |
3 | |
4 | use Test::More; |
c3e9f718 |
5 | use Test::Exception; |
6 | use Test::Warn; |
4ffbc1d6 |
7 | use lib qw(t/lib); |
8 | use DBICTest; |
9 | |
4ffbc1d6 |
10 | # Set up the "usual" sqlite for DBICTest |
11 | my $schema = DBICTest->init_schema; |
12 | |
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: |
16 | |
65d35121 |
17 | my $throw = sub { $schema->resultset("Artist")->search(1,1,1) }; |
4ffbc1d6 |
18 | my $ex_regex = qr/Odd number of arguments to search/; |
19 | |
20 | # Basic check, normal exception |
65d35121 |
21 | throws_ok \&$throw, $ex_regex; |
c3e9f718 |
22 | |
23 | my $e = $@; |
b2f408f3 |
24 | |
25 | # Re-throw the exception with rethrow() |
c3e9f718 |
26 | throws_ok { $e->rethrow } |
27 | $ex_regex; |
b2f408f3 |
28 | isa_ok( $@, 'DBIx::Class::Exception' ); |
4ffbc1d6 |
29 | |
30 | # Now lets rethrow via exception_action |
4f29a592 |
31 | { |
32 | my $handler_execution_counter = 0; |
33 | |
34 | $schema->exception_action(sub { |
35 | $handler_execution_counter++; |
36 | like $_[0], $ex_regex, "Exception is precisely passed to exception_action"; |
37 | die @_ |
38 | }); |
39 | |
40 | throws_ok \&$throw, $ex_regex; |
41 | is $handler_execution_counter, 1, "exception_action handler executed exactly once"; |
42 | } |
4ffbc1d6 |
43 | |
c3e9f718 |
44 | # |
45 | # This should have never worked!!! |
46 | # |
4ffbc1d6 |
47 | # Now lets suppress the error |
48 | $schema->exception_action(sub { 1 }); |
65d35121 |
49 | throws_ok \&$throw, |
c3e9f718 |
50 | qr/exception_action handler .+ did \*not\* result in an exception.+original error: $ex_regex/; |
4ffbc1d6 |
51 | |
52 | # Now lets fall through and let croak take back over |
53 | $schema->exception_action(sub { return }); |
c3e9f718 |
54 | throws_ok { |
65d35121 |
55 | warnings_are \&$throw, |
c3e9f718 |
56 | qr/exception_action handler installed .+ returned false instead throwing an exception/; |
57 | } $ex_regex; |
58 | |
59 | # again to see if no warning |
60 | throws_ok { |
65d35121 |
61 | warnings_are \&$throw, |
c3e9f718 |
62 | []; |
63 | } $ex_regex; |
64 | |
4ffbc1d6 |
65 | |
66 | # Whacky useless exception class |
67 | { |
68 | package DBICTest::Exception; |
69 | use overload '""' => \&stringify, fallback => 1; |
70 | sub new { |
71 | my $class = shift; |
72 | bless { msg => shift }, $class; |
73 | } |
74 | sub throw { |
75 | my $self = shift; |
76 | die $self if ref $self eq __PACKAGE__; |
77 | die $self->new(shift); |
78 | } |
79 | sub stringify { |
80 | "DBICTest::Exception is handling this: " . shift->{msg}; |
81 | } |
82 | } |
83 | |
84 | # Try the exception class |
85 | $schema->exception_action(sub { DBICTest::Exception->throw(@_) }); |
65d35121 |
86 | throws_ok \&$throw, |
c3e9f718 |
87 | qr/DBICTest::Exception is handling this: $ex_regex/; |
4ffbc1d6 |
88 | |
89 | # While we're at it, lets throw a custom exception through Storage::DBI |
c3e9f718 |
90 | throws_ok { $schema->storage->throw_exception('floob') } |
91 | qr/DBICTest::Exception is handling this: floob/; |
9bf06dc0 |
92 | |
68fe9141 |
93 | done_testing; |