| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package IPC::Open3::Callback::CommandFailedException; | ||
| 5 | $IPC::Open3::Callback::CommandFailedException::VERSION = '1.17'; | ||
| 6 | # ABSTRACT: An exception thrown when run_or_die encounters a failed command | ||
| 7 | # PODNAME: IPC::Open3::Callback::CommandFailedException | ||
| 8 | |||
| 9 | use overload q{""} => 'to_string', fallback => 1; | ||
| 10 | use parent qw(Class::Accessor); | ||
| 11 | __PACKAGE__->follow_best_practice; | ||
| 12 | __PACKAGE__->mk_ro_accessors(qw(command exit_status out err)); | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my ( $class, @args ) = @_; | ||
| 16 | return bless( {}, $class )->_init(@args); | ||
| 17 | } | ||
| 18 | |||
| 19 | sub _init { | ||
| 20 | my ( $self, $command, $exit_status, $out, $err ) = @_; | ||
| 21 | |||
| 22 | $self->{command} = $command; | ||
| 23 | $self->{exit_status} = $exit_status; | ||
| 24 | if ( defined($out) ) { | ||
| 25 | $out =~ s/^\s+//; | ||
| 26 | $out =~ s/\s+$//; | ||
| 27 | $self->{out} = $out; | ||
| 28 | } | ||
| 29 | if ( defined($err) ) { | ||
| 30 | $err =~ s/^\s+//; | ||
| 31 | $err =~ s/\s+$//; | ||
| 32 | $self->{err} = $err; | ||
| 33 | } | ||
| 34 | |||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub to_string { | ||
| 39 | my ($self) = @_; | ||
| 40 | if ( !$self->{message} ) { | ||
| 41 | my @message = ( 'FAILED (', $self->{exit_status}, '): ', @{ $self->{command} } ); | ||
| 42 | if ( $self->{out} ) { | ||
| 43 | push( @message, "\n***** out *****\n", $self->{out}, "\n***** end out *****" ); | ||
| 44 | } | ||
| 45 | if ( $self->{err} ) { | ||
| 46 | push( @message, "\n***** err *****\n", $self->{err}, "\n***** end err *****" ); | ||
| 47 | } | ||
| 48 | $self->{message} = join( '', @message ); | ||
| 49 | } | ||
| 50 | return $self->{message}; | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; | ||
| 54 | |||
| 55 | __END__ |