1 package Class::MOP::Exception;
6 use overload '""' => \&to_string;
10 # use Moose; ## Haha, if only.
13 sub message { shift->{message}; }
14 sub stacktrace { shift->{stacktrace}; }
16 # Builder for stacktrace
17 sub _build_stacktrace {
18 shift->{stacktrace} = Devel::StackTrace->new(ignore_class => __PACKAGE__);
26 while (my $frame = $self->{stacktrace}->next_frame) {
29 # message at foo.pl line 1
30 push @lines, sprintf("%s at %s line %s", $self->message, $frame->filename, $frame->line);
32 my @args = map { ref $_ ? "$_" : "'$_'" } $frame->args;
33 # main::foo called at foo.pl line 1
34 push @lines, sprintf("\t%s(%s) called at %s line %s", $frame->subroutine, join(", ", @args), $frame->filename, $frame->line);
42 my ($class, %kwargs) = @_;
43 $class = ref $class if ref $class; # Also take another exception object, if we must.
44 my $message = $kwargs{message} || ''; # Default to no error message
47 my $self = bless {message => $message}, $class;
48 $self->_build_stacktrace;
58 exception - Simple exception class with Stack Trace for Class::MOP
65 sub baz { die(exception->new(message => "Invalid length")) unless @_ == 2}
71 Returns the exception message
75 Returns the Devel::StackTrace object the exception built
77 =head2 to_string (also the auto-stringify method)
79 Returns a Carp::confess-alike formatted stack trace / error string.
81 =head1 CONSTRUCTOR ARGS
85 You may pass in the exception message, designed to be a simple explanation of what went wrong.
89 James Laver L<lt>cpan at jameslaver dot comL<gt>