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