Commit | Line | Data |
4981dc70 |
1 | package DBIx::Class::Exception; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
70c28808 |
6 | use DBIx::Class::Carp (); |
5e0e5426 |
7 | $Carp::Internal{ (__PACKAGE__) }++; |
4981dc70 |
8 | |
9 | use overload |
10 | '""' => sub { shift->{msg} }, |
11 | fallback => 1; |
12 | |
13 | =head1 NAME |
14 | |
15 | DBIx::Class::Exception - Exception objects for DBIx::Class |
16 | |
17 | =head1 DESCRIPTION |
18 | |
289500c2 |
19 | Exception objects of this class are used internally by |
20 | the default error handling of L<DBIx::Class::Schema/throw_exception> |
70c28808 |
21 | and derivatives. |
4981dc70 |
22 | |
23 | These objects stringify to the contained error message, and use |
24 | overload 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 | |
36 | This is meant for internal use by L<DBIx::Class>'s C<throw_exception> |
37 | code, and shouldn't be used directly elsewhere. |
38 | |
39 | Expects a scalar exception message. The optional argument |
70c28808 |
40 | C<$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 | |
47 | sub 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 | |
78 | This method provides some syntactic sugar in order to |
79 | re-throw exceptions. |
80 | |
81 | =cut |
82 | |
83 | sub rethrow { |
84 | die shift; |
85 | } |
86 | |
0c11ad0e |
87 | =head1 AUTHOR AND CONTRIBUTORS |
4981dc70 |
88 | |
0c11ad0e |
89 | See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class |
4981dc70 |
90 | |
91 | =head1 LICENSE |
92 | |
93 | You may distribute this code under the same terms as Perl itself. |
94 | |
95 | =cut |
96 | |
97 | 1; |