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