Merge 'trunk' into 'DBIx-Class-current'
[dbsrgits/DBIx-Class.git] / t / 34exception_action.t
CommitLineData
4ffbc1d6 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
8plan tests => 6;
9
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
21eval { throwex };
22like($@, $ex_regex);
23
24# Now lets rethrow via exception_action
25$schema->exception_action(sub { die @_ });
26eval { throwex };
27like($@, $ex_regex);
28
29# Now lets suppress the error
30$schema->exception_action(sub { 1 });
31eval { throwex };
32ok(!$@, "Suppress exception");
33
34# Now lets fall through and let croak take back over
35$schema->exception_action(sub { return });
36eval { throwex };
37like($@, $ex_regex);
38
39# Whacky useless exception class
40{
41 package DBICTest::Exception;
42 use overload '""' => \&stringify, fallback => 1;
43 sub new {
44 my $class = shift;
45 bless { msg => shift }, $class;
46 }
47 sub throw {
48 my $self = shift;
49 die $self if ref $self eq __PACKAGE__;
50 die $self->new(shift);
51 }
52 sub stringify {
53 "DBICTest::Exception is handling this: " . shift->{msg};
54 }
55}
56
57# Try the exception class
58$schema->exception_action(sub { DBICTest::Exception->throw(@_) });
59eval { throwex };
60like($@, qr/DBICTest::Exception is handling this: $ex_regex/);
61
62# While we're at it, lets throw a custom exception through Storage::DBI
63eval { DBICTest->schema->storage->throw_exception('floob') };
64like($@, qr/DBICTest::Exception is handling this: floob/);