| 1 | # Error.pm | ||
| 2 | # | ||
| 3 | # Copyright (c) 1997-8 Graham Barr <gbarr@ti.com>. All rights reserved. | ||
| 4 | # This program is free software; you can redistribute it and/or | ||
| 5 | # modify it under the same terms as Perl itself. | ||
| 6 | # | ||
| 7 | # Based on my original Error.pm, and Exceptions.pm by Peter Seibel | ||
| 8 | # <peter@weblogic.com> and adapted by Jesse Glick <jglick@sig.bsh.com>. | ||
| 9 | # | ||
| 10 | # but modified ***significantly*** | ||
| 11 | |||
| 12 | package Error; | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use vars qw($VERSION); | ||
| 16 | use 5.004; | ||
| 17 | |||
| 18 | $VERSION = "0.15009"; | ||
| 19 | |||
| 20 | use overload ( | ||
| 21 | '""' => 'stringify', | ||
| 22 | '0+' => 'value', | ||
| 23 | 'bool' => sub { return 1; }, | ||
| 24 | 'fallback' => 1 | ||
| 25 | ); | ||
| 26 | |||
| 27 | $Error::Depth = 0; # Depth to pass to caller() | ||
| 28 | $Error::Debug = 0; # Generate verbose stack traces | ||
| 29 | @Error::STACK = (); # Clause stack for try | ||
| 30 | $Error::THROWN = undef; # last error thrown, a workaround until die $ref works | ||
| 31 | |||
| 32 | my $LAST; # Last error created | ||
| 33 | my %ERROR; # Last error associated with package | ||
| 34 | |||
| 35 | sub throw_Error_Simple | ||
| 36 | { | ||
| 37 | my $args = shift; | ||
| 38 | return Error::Simple->new($args->{'text'}); | ||
| 39 | } | ||
| 40 | |||
| 41 | $Error::ObjectifyCallback = \&throw_Error_Simple; | ||
| 42 | |||
| 43 | |||
| 44 | # Exported subs are defined in Error::subs | ||
| 45 | |||
| 46 | sub import { | ||
| 47 | shift; | ||
| 48 | local $Exporter::ExportLevel = $Exporter::ExportLevel + 1; | ||
| 49 | Error::subs->import(@_); | ||
| 50 | } | ||
| 51 | |||
| 52 | # I really want to use last for the name of this method, but it is a keyword | ||
| 53 | # which prevent the syntax last Error | ||
| 54 | |||
| 55 | sub prior { | ||
| 56 | shift; # ignore | ||
| 57 | |||
| 58 | return $LAST unless @_; | ||
| 59 | |||
| 60 | my $pkg = shift; | ||
| 61 | return exists $ERROR{$pkg} ? $ERROR{$pkg} : undef | ||
| 62 | unless ref($pkg); | ||
| 63 | |||
| 64 | my $obj = $pkg; | ||
| 65 | my $err = undef; | ||
| 66 | if($obj->isa('HASH')) { | ||
| 67 | $err = $obj->{'__Error__'} | ||
| 68 | if exists $obj->{'__Error__'}; | ||
| 69 | } | ||
| 70 | elsif($obj->isa('GLOB')) { | ||
| 71 | $err = ${*$obj}{'__Error__'} | ||
| 72 | if exists ${*$obj}{'__Error__'}; | ||
| 73 | } | ||
| 74 | |||
| 75 | $err; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub flush { | ||
| 79 | shift; #ignore | ||
| 80 | |||
| 81 | unless (@_) { | ||
| 82 | $LAST = undef; | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | my $pkg = shift; | ||
| 87 | return unless ref($pkg); | ||
| 88 | |||
| 89 | undef $ERROR{$pkg} if defined $ERROR{$pkg}; | ||
| 90 | } | ||
| 91 | |||
| 92 | # Return as much information as possible about where the error | ||
| 93 | # happened. The -stacktrace element only exists if $Error::DEBUG | ||
| 94 | # was set when the error was created | ||
| 95 | |||
| 96 | sub stacktrace { | ||
| 97 | my $self = shift; | ||
| 98 | |||
| 99 | return $self->{'-stacktrace'} | ||
| 100 | if exists $self->{'-stacktrace'}; | ||
| 101 | |||
| 102 | my $text = exists $self->{'-text'} ? $self->{'-text'} : "Died"; | ||
| 103 | |||
| 104 | $text .= sprintf(" at %s line %d.\n", $self->file, $self->line) | ||
| 105 | unless($text =~ /\n$/s); | ||
| 106 | |||
| 107 | $text; | ||
| 108 | } | ||
| 109 | |||
| 110 | # Allow error propagation, ie | ||
| 111 | # | ||
| 112 | # $ber->encode(...) or | ||
| 113 | # return Error->prior($ber)->associate($ldap); | ||
| 114 | |||
| 115 | sub associate { | ||
| 116 | my $err = shift; | ||
| 117 | my $obj = shift; | ||
| 118 | |||
| 119 | return unless ref($obj); | ||
| 120 | |||
| 121 | if($obj->isa('HASH')) { | ||
| 122 | $obj->{'__Error__'} = $err; | ||
| 123 | } | ||
| 124 | elsif($obj->isa('GLOB')) { | ||
| 125 | ${*$obj}{'__Error__'} = $err; | ||
| 126 | } | ||
| 127 | $obj = ref($obj); | ||
| 128 | $ERROR{ ref($obj) } = $err; | ||
| 129 | |||
| 130 | return; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub new { | ||
| 134 | my $self = shift; | ||
| 135 | my($pkg,$file,$line) = caller($Error::Depth); | ||
| 136 | |||
| 137 | my $err = bless { | ||
| 138 | '-package' => $pkg, | ||
| 139 | '-file' => $file, | ||
| 140 | '-line' => $line, | ||
| 141 | @_ | ||
| 142 | }, $self; | ||
| 143 | |||
| 144 | $err->associate($err->{'-object'}) | ||
| 145 | if(exists $err->{'-object'}); | ||
| 146 | |||
| 147 | # To always create a stacktrace would be very inefficient, so | ||
| 148 | # we only do it if $Error::Debug is set | ||
| 149 | |||
| 150 | if($Error::Debug) { | ||
| 151 | require Carp; | ||
| 152 | local $Carp::CarpLevel = $Error::Depth; | ||
| 153 | my $text = defined($err->{'-text'}) ? $err->{'-text'} : "Error"; | ||
| 154 | my $trace = Carp::longmess($text); | ||
| 155 | # Remove try calls from the trace | ||
| 156 | $trace =~ s/(\n\s+\S+__ANON__[^\n]+)?\n\s+eval[^\n]+\n\s+Error::subs::try[^\n]+(?=\n)//sog; | ||
| 157 | $trace =~ s/(\n\s+\S+__ANON__[^\n]+)?\n\s+eval[^\n]+\n\s+Error::subs::run_clauses[^\n]+\n\s+Error::subs::try[^\n]+(?=\n)//sog; | ||
| 158 | $err->{'-stacktrace'} = $trace | ||
| 159 | } | ||
| 160 | |||
| 161 | $@ = $LAST = $ERROR{$pkg} = $err; | ||
| 162 | } | ||
| 163 | |||
| 164 | # Throw an error. this contains some very gory code. | ||
| 165 | |||
| 166 | sub throw { | ||
| 167 | my $self = shift; | ||
| 168 | local $Error::Depth = $Error::Depth + 1; | ||
| 169 | |||
| 170 | # if we are not rethrow-ing then create the object to throw | ||
| 171 | $self = $self->new(@_) unless ref($self); | ||
| 172 | |||
| 173 | die $Error::THROWN = $self; | ||
| 174 | } | ||
| 175 | |||
| 176 | # syntactic sugar for | ||
| 177 | # | ||
| 178 | # die with Error( ... ); | ||
| 179 | |||
| 180 | sub with { | ||
| 181 | my $self = shift; | ||
| 182 | local $Error::Depth = $Error::Depth + 1; | ||
| 183 | |||
| 184 | $self->new(@_); | ||
| 185 | } | ||
| 186 | |||
| 187 | # syntactic sugar for | ||
| 188 | # | ||
| 189 | # record Error( ... ) and return; | ||
| 190 | |||
| 191 | sub record { | ||
| 192 | my $self = shift; | ||
| 193 | local $Error::Depth = $Error::Depth + 1; | ||
| 194 | |||
| 195 | $self->new(@_); | ||
| 196 | } | ||
| 197 | |||
| 198 | # catch clause for | ||
| 199 | # | ||
| 200 | # try { ... } catch CLASS with { ... } | ||
| 201 | |||
| 202 | sub catch { | ||
| 203 | my $pkg = shift; | ||
| 204 | my $code = shift; | ||
| 205 | my $clauses = shift || {}; | ||
| 206 | my $catch = $clauses->{'catch'} ||= []; | ||
| 207 | |||
| 208 | unshift @$catch, $pkg, $code; | ||
| 209 | |||
| 210 | $clauses; | ||
| 211 | } | ||
| 212 | |||
| 213 | # Object query methods | ||
| 214 | |||
| 215 | sub object { | ||
| 216 | my $self = shift; | ||
| 217 | exists $self->{'-object'} ? $self->{'-object'} : undef; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub file { | ||
| 221 | my $self = shift; | ||
| 222 | exists $self->{'-file'} ? $self->{'-file'} : undef; | ||
| 223 | } | ||
| 224 | |||
| 225 | sub line { | ||
| 226 | my $self = shift; | ||
| 227 | exists $self->{'-line'} ? $self->{'-line'} : undef; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub text { | ||
| 231 | my $self = shift; | ||
| 232 | exists $self->{'-text'} ? $self->{'-text'} : undef; | ||
| 233 | } | ||
| 234 | |||
| 235 | # overload methods | ||
| 236 | |||
| 237 | sub stringify { | ||
| 238 | my $self = shift; | ||
| 239 | defined $self->{'-text'} ? $self->{'-text'} : "Died"; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub value { | ||
| 243 | my $self = shift; | ||
| 244 | exists $self->{'-value'} ? $self->{'-value'} : undef; | ||
| 245 | } | ||
| 246 | |||
| 247 | package Error::Simple; | ||
| 248 | |||
| 249 | @Error::Simple::ISA = qw(Error); | ||
| 250 | |||
| 251 | sub new { | ||
| 252 | my $self = shift; | ||
| 253 | my $text = "" . shift; | ||
| 254 | my $value = shift; | ||
| 255 | my(@args) = (); | ||
| 256 | |||
| 257 | local $Error::Depth = $Error::Depth + 1; | ||
| 258 | |||
| 259 | @args = ( -file => $1, -line => $2) | ||
| 260 | if($text =~ s/\s+at\s+(\S+)\s+line\s+(\d+)(?:,\s*<[^>]*>\s+line\s+\d+)?\.?\n?$//s); | ||
| 261 | push(@args, '-value', 0 + $value) | ||
| 262 | if defined($value); | ||
| 263 | |||
| 264 | $self->SUPER::new(-text => $text, @args); | ||
| 265 | } | ||
| 266 | |||
| 267 | sub stringify { | ||
| 268 | my $self = shift; | ||
| 269 | my $text = $self->SUPER::stringify; | ||
| 270 | $text .= sprintf(" at %s line %d.\n", $self->file, $self->line) | ||
| 271 | unless($text =~ /\n$/s); | ||
| 272 | $text; | ||
| 273 | } | ||
| 274 | |||
| 275 | ########################################################################## | ||
| 276 | ########################################################################## | ||
| 277 | |||
| 278 | # Inspired by code from Jesse Glick <jglick@sig.bsh.com> and | ||
| 279 | # Peter Seibel <peter@weblogic.com> | ||
| 280 | |||
| 281 | package Error::subs; | ||
| 282 | |||
| 283 | use Exporter (); | ||
| 284 | use vars qw(@EXPORT_OK @ISA %EXPORT_TAGS); | ||
| 285 | |||
| 286 | @EXPORT_OK = qw(try with finally except otherwise); | ||
| 287 | %EXPORT_TAGS = (try => \@EXPORT_OK); | ||
| 288 | |||
| 289 | @ISA = qw(Exporter); | ||
| 290 | |||
| 291 | |||
| 292 | sub blessed { | ||
| 293 | my $item = shift; | ||
| 294 | local $@; # don't kill an outer $@ | ||
| 295 | ref $item and eval { $item->can('can') }; | ||
| 296 | } | ||
| 297 | |||
| 298 | |||
| 299 | sub run_clauses ($$$\@) { | ||
| 300 | my($clauses,$err,$wantarray,$result) = @_; | ||
| 301 | my $code = undef; | ||
| 302 | |||
| 303 | $err = $Error::ObjectifyCallback->({'text' =>$err}) unless ref($err); | ||
| 304 | |||
| 305 | CATCH: { | ||
| 306 | |||
| 307 | # catch | ||
| 308 | my $catch; | ||
| 309 | if(defined($catch = $clauses->{'catch'})) { | ||
| 310 | my $i = 0; | ||
| 311 | |||
| 312 | CATCHLOOP: | ||
| 313 | for( ; $i < @$catch ; $i += 2) { | ||
| 314 | my $pkg = $catch->[$i]; | ||
| 315 | unless(defined $pkg) { | ||
| 316 | #except | ||
| 317 | splice(@$catch,$i,2,$catch->[$i+1]->()); | ||
| 318 | $i -= 2; | ||
| 319 | next CATCHLOOP; | ||
| 320 | } | ||
| 321 | elsif(blessed($err) && $err->isa($pkg)) { | ||
| 322 | $code = $catch->[$i+1]; | ||
| 323 | while(1) { | ||
| 324 | my $more = 0; | ||
| 325 | local($Error::THROWN); | ||
| 326 | my $ok = eval { | ||
| 327 | if($wantarray) { | ||
| 328 | @{$result} = $code->($err,\$more); | ||
| 329 | } | ||
| 330 | elsif(defined($wantarray)) { | ||
| 331 | @{$result} = (); | ||
| 332 | $result->[0] = $code->($err,\$more); | ||
| 333 | } | ||
| 334 | else { | ||
| 335 | $code->($err,\$more); | ||
| 336 | } | ||
| 337 | 1; | ||
| 338 | }; | ||
| 339 | if( $ok ) { | ||
| 340 | next CATCHLOOP if $more; | ||
| 341 | undef $err; | ||
| 342 | } | ||
| 343 | else { | ||
| 344 | $err = defined($Error::THROWN) | ||
| 345 | ? $Error::THROWN : $@; | ||
| 346 | $err = $Error::ObjectifyCallback->({'text' =>$err}) | ||
| 347 | unless ref($err); | ||
| 348 | } | ||
| 349 | last CATCH; | ||
| 350 | }; | ||
| 351 | } | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | # otherwise | ||
| 356 | my $owise; | ||
| 357 | if(defined($owise = $clauses->{'otherwise'})) { | ||
| 358 | my $code = $clauses->{'otherwise'}; | ||
| 359 | my $more = 0; | ||
| 360 | my $ok = eval { | ||
| 361 | if($wantarray) { | ||
| 362 | @{$result} = $code->($err,\$more); | ||
| 363 | } | ||
| 364 | elsif(defined($wantarray)) { | ||
| 365 | @{$result} = (); | ||
| 366 | $result->[0] = $code->($err,\$more); | ||
| 367 | } | ||
| 368 | else { | ||
| 369 | $code->($err,\$more); | ||
| 370 | } | ||
| 371 | 1; | ||
| 372 | }; | ||
| 373 | if( $ok ) { | ||
| 374 | undef $err; | ||
| 375 | } | ||
| 376 | else { | ||
| 377 | $err = defined($Error::THROWN) | ||
| 378 | ? $Error::THROWN : $@; | ||
| 379 | |||
| 380 | $err = $Error::ObjectifyCallback->({'text' =>$err}) | ||
| 381 | unless ref($err); | ||
| 382 | } | ||
| 383 | } | ||
| 384 | } | ||
| 385 | $err; | ||
| 386 | } | ||
| 387 | |||
| 388 | sub try (&;$) { | ||
| 389 | my $try = shift; | ||
| 390 | my $clauses = @_ ? shift : {}; | ||
| 391 | my $ok = 0; | ||
| 392 | my $err = undef; | ||
| 393 | my @result = (); | ||
| 394 | |||
| 395 | unshift @Error::STACK, $clauses; | ||
| 396 | |||
| 397 | my $wantarray = wantarray(); | ||
| 398 | |||
| 399 | do { | ||
| 400 | local $Error::THROWN = undef; | ||
| 401 | local $@ = undef; | ||
| 402 | |||
| 403 | $ok = eval { | ||
| 404 | if($wantarray) { | ||
| 405 | @result = $try->(); | ||
| 406 | } | ||
| 407 | elsif(defined $wantarray) { | ||
| 408 | $result[0] = $try->(); | ||
| 409 | } | ||
| 410 | else { | ||
| 411 | $try->(); | ||
| 412 | } | ||
| 413 | 1; | ||
| 414 | }; | ||
| 415 | |||
| 416 | $err = defined($Error::THROWN) ? $Error::THROWN : $@ | ||
| 417 | unless $ok; | ||
| 418 | }; | ||
| 419 | |||
| 420 | shift @Error::STACK; | ||
| 421 | |||
| 422 | $err = run_clauses($clauses,$err,wantarray,@result) | ||
| 423 | unless($ok); | ||
| 424 | |||
| 425 | $clauses->{'finally'}->() | ||
| 426 | if(defined($clauses->{'finally'})); | ||
| 427 | |||
| 428 | if (defined($err)) | ||
| 429 | { | ||
| 430 | if (blessed($err) && $err->can('throw')) | ||
| 431 | { | ||
| 432 | throw $err; | ||
| 433 | } | ||
| 434 | else | ||
| 435 | { | ||
| 436 | die $err; | ||
| 437 | } | ||
| 438 | } | ||
| 439 | |||
| 440 | wantarray ? @result : $result[0]; | ||
| 441 | } | ||
| 442 | |||
| 443 | # Each clause adds a sub to the list of clauses. The finally clause is | ||
| 444 | # always the last, and the otherwise clause is always added just before | ||
| 445 | # the finally clause. | ||
| 446 | # | ||
| 447 | # All clauses, except the finally clause, add a sub which takes one argument | ||
| 448 | # this argument will be the error being thrown. The sub will return a code ref | ||
| 449 | # if that clause can handle that error, otherwise undef is returned. | ||
| 450 | # | ||
| 451 | # The otherwise clause adds a sub which unconditionally returns the users | ||
| 452 | # code reference, this is why it is forced to be last. | ||
| 453 | # | ||
| 454 | # The catch clause is defined in Error.pm, as the syntax causes it to | ||
| 455 | # be called as a method | ||
| 456 | |||
| 457 | sub with (&;$) { | ||
| 458 | @_ | ||
| 459 | } | ||
| 460 | |||
| 461 | sub finally (&) { | ||
| 462 | my $code = shift; | ||
| 463 | my $clauses = { 'finally' => $code }; | ||
| 464 | $clauses; | ||
| 465 | } | ||
| 466 | |||
| 467 | # The except clause is a block which returns a hashref or a list of | ||
| 468 | # key-value pairs, where the keys are the classes and the values are subs. | ||
| 469 | |||
| 470 | sub except (&;$) { | ||
| 471 | my $code = shift; | ||
| 472 | my $clauses = shift || {}; | ||
| 473 | my $catch = $clauses->{'catch'} ||= []; | ||
| 474 | |||
| 475 | my $sub = sub { | ||
| 476 | my $ref; | ||
| 477 | my(@array) = $code->($_[0]); | ||
| 478 | if(@array == 1 && ref($array[0])) { | ||
| 479 | $ref = $array[0]; | ||
| 480 | $ref = [ %$ref ] | ||
| 481 | if(UNIVERSAL::isa($ref,'HASH')); | ||
| 482 | } | ||
| 483 | else { | ||
| 484 | $ref = \@array; | ||
| 485 | } | ||
| 486 | @$ref | ||
| 487 | }; | ||
| 488 | |||
| 489 | unshift @{$catch}, undef, $sub; | ||
| 490 | |||
| 491 | $clauses; | ||
| 492 | } | ||
| 493 | |||
| 494 | sub otherwise (&;$) { | ||
| 495 | my $code = shift; | ||
| 496 | my $clauses = shift || {}; | ||
| 497 | |||
| 498 | if(exists $clauses->{'otherwise'}) { | ||
| 499 | require Carp; | ||
| 500 | Carp::croak("Multiple otherwise clauses"); | ||
| 501 | } | ||
| 502 | |||
| 503 | $clauses->{'otherwise'} = $code; | ||
| 504 | |||
| 505 | $clauses; | ||
| 506 | } | ||
| 507 | |||
| 508 | 1; | ||
| 509 | __END__ |