File Coverage

blib/lib/RPC/ExtDirect/Exception.pm
Criterion Covered Total %
statement 40 40 100.0
branch 4 4 100.0
condition 3 3 100.0
subroutine 11 11 100.0
pod 0 3 0.0
total 58 61 95.0


line stmt bran cond sub pod time code
1             package RPC::ExtDirect::Exception;
2              
3 7     7   919 use strict;
  7         8  
  7         165  
4 7     7   21 use warnings;
  7         7  
  7         153  
5 7     7   31 no warnings 'uninitialized'; ## no critic
  7         5  
  7         185  
6              
7 7     7   22 use Carp;
  7         6  
  7         1062  
8              
9 7     7   720 use RPC::ExtDirect::Util::Accessor;
  7         10  
  7         160  
10 7     7   729 use RPC::ExtDirect::Util qw/ clean_error_message get_caller_info /;
  7         11  
  7         1963  
11            
12             ### PUBLIC CLASS METHOD (CONSTRUCTOR) ###
13             #
14             # Initializes new instance of Exception.
15             #
16              
17             sub new {
18 25     25 0 2144 my ($class, $arg) = @_;
19              
20 25         30 my $where = $arg->{where};
21 25         33 my $message = $arg->{message};
22              
23             my $self = bless {
24             debug => $arg->{debug},
25             action => $arg->{action},
26             method => $arg->{method},
27             tid => $arg->{tid},
28             verbose => $arg->{verbose},
29 25         104 }, $class;
30              
31 25         51 $self->_set_error($message, $where);
32              
33 25         89 return $self;
34             }
35              
36             ### PUBLIC INSTANCE METHOD ###
37             #
38             # A stub for duck typing. Always returns failure.
39             #
40              
41 18     18 0 5323 sub run { '' }
42              
43             ### PUBLIC INSTANCE METHOD ###
44             #
45             # Returns exception hashref; named so for duck typing.
46             #
47              
48             sub result {
49 25     25 0 10777 my ($self) = @_;
50              
51 25         54 return $self->_get_exception_hashref();
52             }
53              
54             ### PUBLIC INSTANCE METHODS ###
55             #
56             # Simple read-write accessors
57             #
58              
59             RPC::ExtDirect::Util::Accessor::mk_accessors(
60             simple => [qw/
61             debug action method tid where message verbose
62             /],
63             );
64              
65             ############## PRIVATE METHODS BELOW ##############
66              
67             ### PRIVATE INSTANCE METHOD ###
68             #
69             # Sets internal error condition and message
70             #
71              
72             sub _set_error {
73 25     25   34 my ($self, $message, $where) = @_;
74              
75             # Store the information
76 25 100       76 $self->{where} = defined $where ? $where : get_caller_info(3);
77 25         33 $self->{message} = $message;
78              
79             # Ensure fall through for caller methods
80 25         27 return !1;
81             }
82              
83             ### PRIVATE INSTANCE METHOD ###
84             #
85             # Returns exception hashref
86             #
87              
88             sub _get_exception_hashref {
89 25     25   28 my ($self) = @_;
90              
91             # If debug flag is not set, return generic message. This is for
92             # compatibility with Ext.Direct specification.
93 25         23 my ($where, $message);
94            
95 25 100 100     609 if ( $self->debug || $self->verbose ) {
96 19         429 $where = $self->where;
97 19         385 $message = $self->message;
98             }
99             else {
100 6         10 $where = 'ExtDirect';
101 6         8 $message = 'An error has occured while processing request';
102             };
103              
104             # Format the hashref
105 25         488 my $exception_ref = {
106             type => 'exception',
107             action => $self->action,
108             method => $self->method,
109             tid => $self->tid,
110             where => $where,
111             message => $message,
112             };
113              
114 25         70 return $exception_ref;
115             }
116              
117             1;