| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | use lib 'lib'; | ||
| 5 | |||
| 6 | package File::Util::Exception; | ||
| 7 | $File::Util::Exception::VERSION = '4.161950'; | ||
| 8 | # ABSTRACT: Base exception class for File::Util | ||
| 9 | |||
| 10 | use File::Util::Definitions qw( :all ); | ||
| 11 | |||
| 12 | use vars qw( | ||
| 13 | @ISA $AUTHORITY | ||
| 14 | @EXPORT_OK %EXPORT_TAGS | ||
| 15 | ); | ||
| 16 | |||
| 17 | use Exporter; | ||
| 18 | |||
| 19 | $AUTHORITY = 'cpan:TOMMY'; | ||
| 20 | @ISA = qw( Exporter ); | ||
| 21 | @EXPORT_OK = qw( _throw ); | ||
| 22 | %EXPORT_TAGS = ( all => [ @EXPORT_OK ] ); | ||
| 23 | |||
| 24 | |||
| 25 | # -------------------------------------------------------- | ||
| 26 | # File::Util::Exception::_throw | ||
| 27 | # -------------------------------------------------------- | ||
| 28 | sub _throw { | ||
| 29 | |||
| 30 | my @in = @_; | ||
| 31 | my ( $this, $error_class, $error ) = splice @_, 0 , 3; | ||
| 32 | my $opts = $this->_remove_opts( \@_ ); | ||
| 33 | my %fatal_rules = (); | ||
| 34 | |||
| 35 | # here we handle support for the legacy error handling policy syntax, | ||
| 36 | # such as things like "fatals_as_status => 1" | ||
| 37 | # | ||
| 38 | # ...and we also handle support for the newer, more pretty error | ||
| 39 | # handling policy syntax using "onfail" keywords/subrefs | ||
| 40 | |||
| 41 | $opts->{onfail} ||= | ||
| 42 | $opts->{opts} && ref $opts->{opts} eq 'HASH' | ||
| 43 | ? $opts->{opts}->{onfail} | ||
| 44 | : ''; | ||
| 45 | |||
| 46 | $opts->{onfail} ||= $this->{opts}->{onfail}; | ||
| 47 | |||
| 48 | $opts->{onfail} ||= 'die'; | ||
| 49 | |||
| 50 | # fatalality-handling rules passed to the failing caller trump the | ||
| 51 | # rules set up in the attributes of the object; the mechanism below | ||
| 52 | # also allows for the implicit handling of fatals_are_fatal => 1 | ||
| 53 | map { $fatal_rules{ $_ } = $_ } | ||
| 54 | grep /^fatals/o, keys %$opts; | ||
| 55 | |||
| 56 | map { $fatal_rules{ $_ } = $_ } | ||
| 57 | grep /^fatals/o, keys %{ $opts->{opts} } | ||
| 58 | if $opts->{opts} && ref $opts->{opts} eq 'HASH'; | ||
| 59 | |||
| 60 | unless ( scalar keys %fatal_rules ) { | ||
| 61 | map { $fatal_rules{ $_ } = $_ } | ||
| 62 | grep /^fatals/o, keys %{ $this->{opts} } | ||
| 63 | } | ||
| 64 | |||
| 65 | return 0 if $fatal_rules{fatals_as_status} || $opts->{onfail} eq 'zero'; | ||
| 66 | |||
| 67 | return if $opts->{onfail} eq 'undefined'; | ||
| 68 | |||
| 69 | my $is_plain; | ||
| 70 | |||
| 71 | if ( !scalar keys %$opts ) { | ||
| 72 | |||
| 73 | $opts->{_pak} = 'File::Util'; | ||
| 74 | |||
| 75 | $opts->{error} = $error; | ||
| 76 | |||
| 77 | $error = $error ? 'plain error' : 'empty error'; | ||
| 78 | |||
| 79 | $is_plain++; | ||
| 80 | } | ||
| 81 | else { | ||
| 82 | |||
| 83 | $opts->{_pak} = 'File::Util'; | ||
| 84 | |||
| 85 | $error ||= 'empty error'; | ||
| 86 | |||
| 87 | if ( $error eq 'plain error' ) { | ||
| 88 | |||
| 89 | $opts->{error} ||= shift @_; | ||
| 90 | |||
| 91 | $is_plain++; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | my $bad_news = CORE::eval # tokenizing via stringy eval (is NOT evil) | ||
| 96 | ( | ||
| 97 | '<<__ERRBLOCK__' . NL . | ||
| 98 | $error_class->_errors( $error ) . NL . | ||
| 99 | '__ERRBLOCK__' | ||
| 100 | ); | ||
| 101 | |||
| 102 | if ( | ||
| 103 | $opts->{onfail} eq 'warn' || | ||
| 104 | $fatal_rules{fatals_as_warning} | ||
| 105 | ) { | ||
| 106 | warn _trace( $@ || $bad_news ) and return; | ||
| 107 | } | ||
| 108 | elsif ( | ||
| 109 | $opts->{onfail} eq 'message' || | ||
| 110 | $fatal_rules{fatals_as_errmsg} || | ||
| 111 | $opts->{return} | ||
| 112 | ) { | ||
| 113 | return _trace( $@ || $bad_news ); | ||
| 114 | } | ||
| 115 | |||
| 116 | warn _trace( $@ || $bad_news ) if $opts->{warn_also}; | ||
| 117 | |||
| 118 | die _trace( $@ || $bad_news ) | ||
| 119 | unless ref $opts->{onfail} eq 'CODE'; | ||
| 120 | |||
| 121 | @_ = ( $bad_news, _trace() ); | ||
| 122 | |||
| 123 | goto $opts->{onfail}; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | # -------------------------------------------------------- | ||
| 129 | # File::Util::Exception::_trace | ||
| 130 | # -------------------------------------------------------- | ||
| 131 | sub _trace { # <<<<< this is not a class or object method! | ||
| 132 | my @errors = @_; | ||
| 133 | |||
| 134 | my | ||
| 135 | ( | ||
| 136 | $pak, $file, $line, $sub, | ||
| 137 | $hasargs, $wantarray, $evaltext, $req_OR_use, | ||
| 138 | @stack, $i, $frame_no | ||
| 139 | ); | ||
| 140 | |||
| 141 | $frame_no = 0; | ||
| 142 | |||
| 143 | while | ||
| 144 | ( | ||
| 145 | ( $pak, $file, $line, $sub, | ||
| 146 | $hasargs, $wantarray, $evaltext, $req_OR_use | ||
| 147 | ) = caller( $i++ ) | ||
| 148 | ) | ||
| 149 | { | ||
| 150 | $frame_no = $i - 2; | ||
| 151 | |||
| 152 | next unless $frame_no > 0; | ||
| 153 | |||
| 154 | push @stack, <<__ERR__ | ||
| 155 | $frame_no. $sub | ||
| 156 | -called at line ($line) of $file | ||
| 157 | @{[ $hasargs | ||
| 158 | ? '-was called with args' | ||
| 159 | : '-was called without args' ]} | ||
| 160 | @{[ $evaltext | ||
| 161 | ? '-was called to evalate text' | ||
| 162 | : '-was not called to evaluate anything' ]} | ||
| 163 | __ERR__ | ||
| 164 | } | ||
| 165 | |||
| 166 | $i = 0; | ||
| 167 | |||
| 168 | for my $error ( @errors ) { | ||
| 169 | |||
| 170 | $error = '' unless defined $error; | ||
| 171 | |||
| 172 | if ( !length $error ) { | ||
| 173 | |||
| 174 | $error = qq{Something is wrong. Frame no. $frame_no...} | ||
| 175 | } | ||
| 176 | |||
| 177 | ++$i; | ||
| 178 | } | ||
| 179 | |||
| 180 | chomp for @errors; | ||
| 181 | |||
| 182 | return join NL, @errors, @stack; | ||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | # -------------------------------------------------------- | ||
| 187 | # File::Util::Exception::DESTROY() | ||
| 188 | # -------------------------------------------------------- | ||
| 189 | sub DESTROY { } | ||
| 190 | |||
| 191 | |||
| 192 | 1; | ||
| 193 | |||
| 194 | |||
| 195 | __END__ |