release 0.08123
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Exception.pm
CommitLineData
4981dc70 1package DBIx::Class::Exception;
2
3use strict;
4use warnings;
5
9780718f 6use Carp::Clan qw/^DBIx::Class|^Try::Tiny/;
4981dc70 7use Scalar::Util qw/blessed/;
bca6956d 8use Try::Tiny;
fd323bf1 9use namespace::clean;
4981dc70 10
11use overload
12 '""' => sub { shift->{msg} },
13 fallback => 1;
14
15=head1 NAME
16
17DBIx::Class::Exception - Exception objects for DBIx::Class
18
19=head1 DESCRIPTION
20
289500c2 21Exception objects of this class are used internally by
22the default error handling of L<DBIx::Class::Schema/throw_exception>
4981dc70 23to prevent confusing and/or redundant re-application of L<Carp>'s
24stack trace information.
25
26These objects stringify to the contained error message, and use
27overload fallback to give natural boolean/numeric values.
28
29=head1 METHODS
30
31=head2 throw
32
33=over 4
34
35=item Arguments: $exception_scalar, $stacktrace
36
37=back
38
39This is meant for internal use by L<DBIx::Class>'s C<throw_exception>
40code, and shouldn't be used directly elsewhere.
41
42Expects a scalar exception message. The optional argument
43C<$stacktrace> tells it to use L<Carp/longmess> instead of
44L<Carp::Clan/croak>.
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
55 die $msg if blessed($msg);
4981dc70 56
57 # use Carp::Clan's croak if we're not stack tracing
58 if(!$stacktrace) {
bca6956d 59 try { croak $msg } catch { $msg = shift };
4981dc70 60 }
61 else {
62 $msg = Carp::longmess($msg);
63 }
d4daee7b 64
4981dc70 65 my $self = { msg => $msg };
66 bless $self => $class;
67
68 die $self;
69}
70
b2f408f3 71=head2 rethrow
72
73This method provides some syntactic sugar in order to
74re-throw exceptions.
75
76=cut
77
78sub rethrow {
79 die shift;
80}
81
4981dc70 82=head1 AUTHORS
83
84Brandon L. Black <blblack@gmail.com>
85
86=head1 LICENSE
87
88You may distribute this code under the same terms as Perl itself.
89
90=cut
91
921;