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