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