File Coverage

blib/lib/POE/Component/Client/opentick/Error.pm
Criterion Covered Total %
statement 61 67 91.0
branch 14 20 70.0
condition 2 3 66.6
subroutine 18 21 85.7
pod 12 12 100.0
total 107 123 86.9


line stmt bran cond sub pod time code
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 5     5   28 use strict;
  5         9  
  5         185  
15 5     5   26 use warnings;
  5         10  
  5         144  
16 5     5   25 use Carp qw( carp croak confess );
  5         6  
  5         420  
17             #$Carp::CarpLevel = 10;
18 5     5   25 use Data::Dumper;
  5         8  
  5         201  
19              
20             # Ours.
21 5     5   1756 use POE::Component::Client::opentick::Constants;
  5         17  
  5         898  
22              
23 5     5   33 use overload '""' => \&stringify;
  5         9  
  5         59  
24              
25             ###
26             ### Variables
27             ###
28              
29 5     5   447 use vars qw( $VERSION $TRUE $FALSE );
  5         11  
  5         769  
30              
31             BEGIN {
32 5     5   27 require Exporter;
33 5         92 our @ISA = qw( Exporter );
34 5         14 our @EXPORT = qw( throw );
35 5         4704 ($VERSION) = q$Revision: 56 $ =~ /(\d+)/;
36             }
37              
38             *TRUE = \1;
39             *FALSE = \0;
40              
41             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             {
55 2     2 1 20 my( $class, @data ) = @_;
56 2 50       7 croak( "$class requires an even number of parameters" ) if( @data & 1 );
57            
58 2         8 my $self = {
59             stack => _process_longmess(),
60             };
61              
62 2         8 bless( $self, $class );
63              
64 2         9 $self->initialize( @data );
65              
66 2         15 return( $self );
67             }
68              
69             sub initialize
70             {
71 2     2 1 9 my( $self, %args ) = @_;
72              
73 2         6 for( keys( %args ) )
74             {
75 4 50       95 $self->{lc $_} = $args{$_} if( $valid_args->{lc $_} );
76             }
77              
78 2 50 66     30 croak( "At least one of Message or Data must be specified!" )
79             unless( exists( $self->{message} ) or exists( $self->{data} ) );
80              
81 2         6 return;
82             }
83              
84             # Dump the object contents appropriately
85             sub stringify
86             {
87 2     2 1 5 my( $self ) = @_;
88              
89 2         6 my $message = $self->get_message();
90 2 100       8 unless( $message )
91             {
92 1         5 my( $errcode, $errmsg ) = $self->get_data();
93 1         5 $message = 'Protocol error ' . $errcode . ': ' . $errmsg;
94             }
95              
96             # Add additional fields
97 2 100       9 if( my( $cmd_name, $cmd_id )= $self->get_command() )
98             {
99 1         6 $message .= sprintf( "\nOTCommand: %d (%s)", $cmd_id, $cmd_name );
100             }
101 2 100       8 $message .= "\nRequest ID: " . $self->get_request_id()
102             if( $self->get_request_id() );
103 2 50       7 $message .= "\n" . $self->get_stack() if( $self->dump_stack() );
104              
105 2         13 return( $message );
106             }
107              
108             # Just give up already, already.
109             sub throw
110             {
111 1     1 1 2 my( $item ) = @_;
112              
113 1         3 my $message = "$item"; # OMG HAX
114              
115 1         175 confess( $message );
116             }
117              
118             ###
119             ### Accessors
120             ###
121              
122             sub set_dump_stack
123             {
124 0     0 1 0 my( $self ) = @_;
125              
126 0         0 $self->{dumpstack} = $TRUE;
127              
128 0         0 return( $self );
129             }
130              
131             sub get_command
132             {
133 2     2 1 4 my( $self ) = @_;
134              
135 2 100       63 return unless $self->{commandid};
136              
137 1         6 return( OTCommand( $self->{commandid} ), $self->{commandid} );
138             }
139              
140             sub get_command_id
141             {
142 0     0 1 0 my( $self ) = @_;
143              
144 0         0 return( $self->{commandid} );
145             }
146              
147             sub dump_stack
148             {
149 2 50   2 1 14 return( shift->{dumpstack} ? $TRUE : $FALSE );
150             }
151              
152             sub get_stack
153             {
154 0     0 1 0 return( shift->{stack} );
155             }
156              
157             sub get_data
158             {
159 1     1 1 2 my( $self ) = @_;
160              
161 1         6 my( $errcode, undef, $errmsg )
162             = unpack( OTTemplate( 'ERROR' ), $self->{data} );
163              
164             return( wantarray
165 1 50       6 ? ( $errcode, $errmsg )
166             : $self->{data} );
167             }
168              
169             sub get_message
170             {
171 2     2 1 5 return( shift->{message} );
172             }
173              
174             sub get_request_id
175             {
176 3     3 1 11 return( shift->{requestid} );
177             }
178              
179             ###
180             ### Private methods
181             ###
182              
183             sub _process_longmess
184             {
185 2     2   456 my @good = grep { ! /(?:Kernel|Session)/ } Carp::longmess();
  2         11  
186              
187 2         10 return( join( "\n", @good ) );
188             }
189              
190             1;
191              
192             __END__