opentick/Error.pm

filter: Statementstmt 91.5% 65 / 71 Branchbran 70.0% 14 / 20 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 85.7% 18 / 21 Podpod 100% 12 / 12 totaltotal 86.0% 111 / 129 CC 13 SCAR 9.6
CC
13
Cov
86%
initialize
5.4
stringify
5.0
dump_stack
2.1
CRAP 2.6
« opentick/Output.pm ↑ Summary opentick/Util.pm »
1 package POE::Component::Client::opentick::Error;
2 #
3 # opentick.com POE client
4 #
5 # Error handling functionality
6 #
7 # infi/2008
8 #
9 # $Id: Error.pm 56 2009-01-08 16:51:14Z infidel $
10 #
11 # Full POD documentation after __END__
12 #
13  
14 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Carp qw( carp croak confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 #$Carp::CarpLevel = 10;
18 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # Ours.
21 6 ▶use POE::Component::Client::opentick::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 6 ▶use overload '""' => \&stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 ###
26 ### Variables
27 ###
28  
29 6 ▶use vars qw( $VERSION $TRUE $FALSE );
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 require Exporter;
33 6 our @ISA = qw( Exporter );
34 6 our @EXPORT = qw( throw );
35 6 ($VERSION) = q$Revision: 56 $ =~ /(\d+)/;
36 }
37  
38 6 *TRUE = \1;
39 6 *FALSE = \0;
40  
41 6 my $valid_args = {
42 requestid => $TRUE,
43 commandid => $TRUE,
44 dumpstack => $TRUE,
45 message => $TRUE,
46 data => $TRUE,
47 };
48  
49 ###
50 ### Public methods
51 ###
52  
53 sub new
54 2 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod documented
55 2 my( $class, @data ) = @_;
56 2 ▶ croak( "$class requires an even number of parameters" ) if( @data & 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @data & 1 0 2 2
57
58 2 my $self = {
59 stack => _process_longmess(),
60 };
61  
62 2 bless( $self, $class );
63  
64 2 $self->initialize( @data );
65  
66 2 return( $self );
67 }
68  
69 sub initialize
70 2 ▶{
Subroutine initialize: called CC=5 SCAR=16.8
Pod documented
71 2 my( $self, %args ) = @_;
72  
73 2 for( keys( %args ) )
74 {
75 4 ▶ $self->{lc $_} = $args{$_} if( $valid_args->{lc $_} );
Branch: 1 branch
BranchTrueFalseTotal
if $$valid_args{lc $_} 4 0 4
76 }
77  
78 croak( "At least one of Message or Data must be specified!" )
79 2 ▶ unless( exists( $self->{message} ) or exists( $self->{data} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"message"} or exists $self->{"data"} 0 2 2
Condition66%2/3
exists $self->{"message"} or exists $self->{"data"}
l!l&&r!l&&!r
110
MC/DC0%0/2
exists $self->{"message"} or exists $self->{"data"}
exists $self->{"message"} exists $self->{"data"}
Truth table66%2/3
A or B
A= exists $self->{"message"}B= exists $self->{"data"}
ABresult
1X1
011
000
80  
81 2 return;
82 }
83  
84 # Dump the object contents appropriately
85 sub stringify
86 2 ▶{
Subroutine stringify: called CC=5 SCAR=16.1
Pod documented
87 2 my( $self ) = @_;
88  
89 2 my $message = $self->get_message();
90 2 ▶ unless( $message )
Branch: 1 branch
BranchTrueFalseTotal
unless ($message) 1 1 2
91 {
92 1 my( $errcode, $errmsg ) = $self->get_data();
93 1 $message = 'Protocol error ' . $errcode . ': ' . $errmsg;
94 }
95  
96 # Add additional fields
97 2 ▶ if( my( $cmd_name, $cmd_id )= $self->get_command() )
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->get_command) 1 1 2
98 {
99 1 $message .= sprintf( "\nOTCommand: %d (%s)", $cmd_id, $cmd_name );
100 }
101 2 ▶ $message .= "\nRequest ID: " . $self->get_request_id()
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_request_id 1 1 2
102 if( $self->get_request_id() );
103 2 ▶ $message .= "\n" . $self->get_stack() if( $self->dump_stack() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->dump_stack 0 2 2
104  
105 2 return( $message );
106 }
107  
108 # Just give up already, already.
109 sub throw
110 1 ▶{
Subroutine throw: called CC=1 SCAR=0.0
Pod documented
111 1 my( $item ) = @_;
112  
113 1 my $message = "$item"; # OMG HAX
114  
115 1 confess( $message );
116 }
117  
118 ###
119 ### Accessors
120 ###
121  
122 sub set_dump_stack
123 0 ▶{
Subroutine set_dump_stack: not called CC=1 SCAR=6.9
Pod documented
124 0 my( $self ) = @_;
125  
126 0 $self->{dumpstack} = $TRUE;
127  
128 0 return( $self );
129 }
130  
131 sub get_command
132 2 ▶{
Subroutine get_command: called CC=2 SCAR=6.9
Pod documented
133 2 my( $self ) = @_;
134  
135 2 ▶ return unless $self->{commandid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"commandid"} 1 1 2
136  
137 1 return( OTCommand( $self->{commandid} ), $self->{commandid} );
138 }
139  
140 sub get_command_id
141 0 ▶{
Subroutine get_command_id: not called CC=1 SCAR=6.9
Pod documented
142 0 my( $self ) = @_;
143  
144 0 return( $self->{commandid} );
145 }
146  
147 sub dump_stack
148 2 ▶{
Subroutine dump_stack: called CC=2 SCAR=7.6
Pod documented
149 2 ▶ return( shift->{dumpstack} ? $TRUE : $FALSE );
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{'dumpstack'} ? : 0 2 2
150 }
151  
152 sub get_stack
153 0 ▶{
Subroutine get_stack: not called CC=1 SCAR=6.9
Pod documented
154 0 return( shift->{stack} );
155 }
156  
157 sub get_data
158 1 ▶{
Subroutine get_data: called CC=2 SCAR=7.1
Pod documented
159 1 my( $self ) = @_;
160  
161 my( $errcode, undef, $errmsg )
162 1 = unpack( OTTemplate( 'ERROR' ), $self->{data} );
163  
164 return( wantarray
165 ? ( $errcode, $errmsg )
166 1 ▶ : $self->{data} );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
167 }
168  
169 sub get_message
170 2 ▶{
Subroutine get_message: called CC=1 SCAR=0.0
Pod documented
171 2 return( shift->{message} );
172 }
173  
174 sub get_request_id
175 3 ▶{
Subroutine get_request_id: called CC=1 SCAR=0.0
Pod documented
176 3 return( shift->{requestid} );
177 }
178  
179 ###
180 ### Private methods
181 ###
182  
183 sub _process_longmess
184 2 ▶{
Subroutine _process_longmess: called CC=1 SCAR=0.0
185 2 my @good = grep { ! /(?:Kernel|Session)/ } Carp::longmess();
186  
187 2 return( join( "\n", @good ) );
188 }
189  
190 6 1;
191  
192 __END__
« opentick/Output.pm ↑ Summary opentick/Util.pm »