| 1 | package Reply::Plugin::CollapseStack; | ||
| 2 | our $AUTHORITY = 'cpan:DOY'; | ||
| 3 | $Reply::Plugin::CollapseStack::VERSION = '0.42'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | # ABSTRACT: display error stack traces only on demand | ||
| 7 | |||
| 8 | use base 'Reply::Plugin'; | ||
| 9 | |||
| 10 | { | ||
| 11 | local @SIG{qw(__DIE__ __WARN__)}; | ||
| 12 | require Carp::Always; | ||
| 13 | } | ||
| 14 | |||
| 15 | |||
| 16 | sub new { | ||
| 17 | my $class = shift; | ||
| 18 | my %opts = @_; | ||
| 19 | |||
| 20 | my $self = $class->SUPER::new(@_); | ||
| 21 | $self->{num_lines} = $opts{num_lines} || 1; | ||
| 22 | |||
| 23 | return $self; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub compile { | ||
| 27 | my $self = shift; | ||
| 28 | my ($next, @args) = @_; | ||
| 29 | |||
| 30 | local $SIG{__DIE__} = \&Carp::Always::_die; | ||
| 31 | $next->(@args); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub execute { | ||
| 35 | my $self = shift; | ||
| 36 | my ($next, @args) = @_; | ||
| 37 | |||
| 38 | local $SIG{__DIE__} = \&Carp::Always::_die; | ||
| 39 | $next->(@args); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub mangle_error { | ||
| 43 | my $self = shift; | ||
| 44 | my $error = shift; | ||
| 45 | |||
| 46 | $self->{full_error} = $error; | ||
| 47 | |||
| 48 | my @lines = split /\n/, $error; | ||
| 49 | if (@lines > $self->{num_lines}) { | ||
| 50 | splice @lines, $self->{num_lines}; | ||
| 51 | $error = join "\n", @lines, " (Run #stack to see the full trace)\n"; | ||
| 52 | } | ||
| 53 | |||
| 54 | return $error; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub command_stack { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | # XXX should use print_error here | ||
| 61 | print($self->{full_error} || "No stack to display.\n"); | ||
| 62 | |||
| 63 | return ''; | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | 1; | ||
| 68 | |||
| 69 | __END__ |