Call mro reinit in tests only on 5.8
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Exception.pm
CommitLineData
4981dc70 1package DBIx::Class::Exception;
2
3use strict;
4use warnings;
5
8c49cf15 6# load Carp early to prevent tickling of the ::Internal stash being
7# interpreted as "Carp is already loaded" by some braindead loader
8use Carp ();
5e0e5426 9$Carp::Internal{ (__PACKAGE__) }++;
4981dc70 10
8c49cf15 11use DBIx::Class::Carp ();
12
4981dc70 13use overload
14 '""' => sub { shift->{msg} },
15 fallback => 1;
16
17=head1 NAME
18
19DBIx::Class::Exception - Exception objects for DBIx::Class
20
21=head1 DESCRIPTION
22
289500c2 23Exception objects of this class are used internally by
24the default error handling of L<DBIx::Class::Schema/throw_exception>
70c28808 25and derivatives.
4981dc70 26
27These objects stringify to the contained error message, and use
28overload fallback to give natural boolean/numeric values.
29
30=head1 METHODS
31
32=head2 throw
33
34=over 4
35
36=item Arguments: $exception_scalar, $stacktrace
37
38=back
39
40This is meant for internal use by L<DBIx::Class>'s C<throw_exception>
41code, and shouldn't be used directly elsewhere.
42
43Expects a scalar exception message. The optional argument
70c28808 44C<$stacktrace> tells it to output a full trace similar to L<Carp/confess>.
4981dc70 45
46 DBIx::Class::Exception->throw('Foo');
bca6956d 47 try { ... } catch { DBIx::Class::Exception->throw(shift) }
4981dc70 48
49=cut
50
51sub throw {
52 my ($class, $msg, $stacktrace) = @_;
53
02ddab6d 54 # Don't re-encapsulate exception objects of any kind
c6d30d5e 55 die $msg if ref($msg);
4981dc70 56
70c28808 57 # all exceptions include a caller
58 $msg =~ s/\n$//;
59
4981dc70 60 if(!$stacktrace) {
70c28808 61 # skip all frames that match the original caller, or any of
62 # the dbic-wide classdata patterns
63 my ($ln, $calling) = DBIx::Class::Carp::__find_caller(
64 '^' . caller() . '$',
65 'DBIx::Class',
66 );
67
68 $msg = "${calling}${msg} ${ln}\n";
4981dc70 69 }
70 else {
71 $msg = Carp::longmess($msg);
72 }
d4daee7b 73
4981dc70 74 my $self = { msg => $msg };
75 bless $self => $class;
76
77 die $self;
78}
79
b2f408f3 80=head2 rethrow
81
82This method provides some syntactic sugar in order to
83re-throw exceptions.
84
85=cut
86
87sub rethrow {
88 die shift;
89}
90
0c11ad0e 91=head1 AUTHOR AND CONTRIBUTORS
4981dc70 92
0c11ad0e 93See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
4981dc70 94
95=head1 LICENSE
96
97You may distribute this code under the same terms as Perl itself.
98
99=cut
100
1011;