| 1 | package SPOPS::Exception; | ||||||||||
| 2 | |||||||||||
| 3 | # $Id: Exception.pm,v 3.5 2004/06/02 00:48:21 lachoy Exp $ | ||||||||||
| 4 | |||||||||||
| 5 | 24 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 24 | ▶ | use base qw( Class::Accessor Exporter ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 24 | ▶ | use overload '""' => \&stringify; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 24 | ▶ | use Devel::StackTrace; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 24 | ▶ | use SPOPS::Error; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 24 | $SPOPS::Exception::VERSION = sprintf("%d.%02d", q$Revision: 3.5 $ =~ /(\d+)\.(\d+)/); | |||||||||
| 12 | 24 | @SPOPS::Exception::EXPORT_OK = qw( spops_error ); | |||||||||
| 13 | |||||||||||
| 14 | 24 | ▶ | use constant DEBUG => 0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 24 | my @STACK = (); | |||||||||
| 17 | 24 | my @FIELDS = qw( message package filename line method trace ); | |||||||||
| 18 | 24 | SPOPS::Exception->mk_accessors( @FIELDS ); | |||||||||
| 19 | |||||||||||
| 20 | ######################################## | ||||||||||
| 21 | # SHORTCUT | ||||||||||
| 22 | |||||||||||
| 23 | 0 | ▶ | sub spops_error { goto &throw( 'SPOPS::Exception', @_ ) } | ||||||||
|
Subroutine
spops_error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | ######################################## | ||||||||||
| 27 | # CLASS METHODS | ||||||||||
| 28 | |||||||||||
| 29 | 5 | ▶ | sub throw { | ||||||||
|
Subroutine
throw: called CC=6 SCAR=18.8
Pod
undocumented
| |||||||||||
| 30 | 5 | my ( $class, @message ) = @_; | |||||||||
| 31 | |||||||||||
| 32 | 5 | ▶ | if ( ref $message[0] ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | my $rethrown = $message[0]; | |||||||||
| 34 | 0 | ▶ | if ( UNIVERSAL::isa( $rethrown, __PACKAGE__ ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | die $rethrown; | |||||||||
| 36 | } | ||||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 5 | ▶ | my $params = ( ref $message[-1] eq 'HASH' ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | ? pop( @message ) : {}; | ||||||||||
| 41 | 5 | my $msg = join( '', @message ); | |||||||||
| 42 | |||||||||||
| 43 | 5 | my $self = bless( {}, $class ); | |||||||||
| 44 | |||||||||||
| 45 | # Do all the fields | ||||||||||
| 46 | |||||||||||
| 47 | 5 | foreach my $field ( $self->get_fields ) { | |||||||||
| 48 | 40 | ▶ | $self->$field( $params->{ $field } ) if ( $params->{ $field } ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | # Now do the message and the initial trace stuff | ||||||||||
| 52 | |||||||||||
| 53 | 5 | $self->message( $msg ); | |||||||||
| 54 | |||||||||||
| 55 | 5 | my @initial_call = caller; | |||||||||
| 56 | 5 | $self->package( $initial_call[0] ); | |||||||||
| 57 | 5 | $self->filename( $initial_call[1] ); | |||||||||
| 58 | 5 | $self->line( $initial_call[2] ); | |||||||||
| 59 | |||||||||||
| 60 | # Grab the method name separately, since the subroutine call | ||||||||||
| 61 | # doesn't seem to be matched up properly with the other caller() | ||||||||||
| 62 | # stuff when we do caller(0). Weird. | ||||||||||
| 63 | |||||||||||
| 64 | 5 | my @added_call = caller(1); | |||||||||
| 65 | 5 | $added_call[3] =~ s/^.*:://; | |||||||||
| 66 | 5 | $self->method( $added_call[3] ); | |||||||||
| 67 | |||||||||||
| 68 | 5 | $self->trace( Devel::StackTrace->new ); | |||||||||
| 69 | |||||||||||
| 70 | 5 | DEBUG && warn "[$class] thrown: ", $self->message, "\n"; | |||||||||
| 71 | |||||||||||
| 72 | 5 | $self->initialize( $params ); | |||||||||
| 73 | |||||||||||
| 74 | 5 | push @STACK, $self; | |||||||||
| 75 | |||||||||||
| 76 | # BACKWARDS COMPATIBILITY (will remove before 1.0) | ||||||||||
| 77 | |||||||||||
| 78 | 5 | $self->fill_error_variables; | |||||||||
| 79 | |||||||||||
| 80 | 5 | die $self; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 5 | ▶ | sub initialize {} | ||||||||
|
Subroutine
initialize: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 84 | |||||||||||
| 85 | 5 | ▶ | sub get_fields { return @FIELDS } | ||||||||
|
Subroutine
get_fields: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 86 | |||||||||||
| 87 | 4 | ▶ | sub get_stack { return @STACK } | ||||||||
|
Subroutine
get_stack: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 88 | 0 | ▶ | sub clear_stack { @STACK = () } | ||||||||
|
Subroutine
clear_stack: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 89 | |||||||||||
| 90 | |||||||||||
| 91 | ######################################## | ||||||||||
| 92 | # OBJECT METHODS | ||||||||||
| 93 | |||||||||||
| 94 | 0 | ▶ | sub creation_location { | ||||||||
|
Subroutine
creation_location: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 95 | 0 | my ( $self ) = @_; | |||||||||
| 96 | 0 | return 'Created in package [' . $self->package . '] ' . | |||||||||
| 97 | 'in method [' . $self->method . ']; ' . | ||||||||||
| 98 | 'at file [' . $self->filename . '] ' . | ||||||||||
| 99 | 'at line [' . $self->line . ']'; | ||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 4 | ▶ | sub stringify { return $_[0]->to_string() } | ||||||||
|
Subroutine
stringify: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 103 | |||||||||||
| 104 | 2 | ▶ | sub to_string { | ||||||||
|
Subroutine
to_string: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||
| 105 | 2 | my ( $self ) = @_; | |||||||||
| 106 | 2 | my $class = ref $self; | |||||||||
| 107 | 2 | ▶ | return "Invalid -- not called from object." unless ( $class ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | |||||||||||
| 109 | 24 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 110 | 2 | ▶ | return $self->message() unless ( ${ $class . '::ShowTrace' } ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 0 | return join( "\n", $self->message, $self->trace->as_string ); | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | # BACKWARDS COMPATIBILITY (will remove before 1.0) | ||||||||||
| 115 | |||||||||||
| 116 | 5 | ▶ | sub fill_error_variables { | ||||||||
|
Subroutine
fill_error_variables: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 117 | 5 | my ( $self ) = @_; | |||||||||
| 118 | 5 | SPOPS::Error->set({ user_msg => $self->message, system_msg => $self->message, | |||||||||
| 119 | package => $self->package, method => $self->method, | ||||||||||
| 120 | filename => $self->filename, line => $self->line }); | ||||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 24 | 1; | |||||||||
| 124 | |||||||||||
| 125 | __END__ | ||||||||||