File Coverage

blib/lib/WWW/FBX/Error.pm
Criterion Covered Total %
statement 20 39 51.2
branch 0 6 0.0
condition 0 18 0.0
subroutine 7 13 53.8
pod 0 3 0.0
total 27 79 34.1


line stmt bran cond sub pod time code
1             package WWW::FBX::Error;
2 23     23   469 use 5.014001;
  23         59  
3 23     23   86 use Moose;
  23         27  
  23         161  
4 23     23   103315 use Try::Tiny;
  23         31  
  23         1404  
5 23     23   9962 use Devel::StackTrace;
  23         52597  
  23         737  
6              
7             #Stringify using error sub
8 23         103 use overload '""' => \&error,
9 23     23   104 'fallback' => 1;
  23         25  
10              
11 23     23   9949 use namespace::autoclean;
  23         133852  
  23         74  
12              
13              
14             has fbx_error => ( is => 'rw', predicate => 'has_fbx_error' );
15             has http_response => ( isa => 'HTTP::Response', is => 'rw', required => 1, handles => [qw/code message/] );
16             has stack_trace => ( is => 'ro', init_arg => undef, builder => '_build_stack_trace' );
17             has _stringified => ( is => 'rw', init_arg => undef, default => undef );
18              
19             sub _build_stack_trace {
20 0     0     my $seen;
21 0           my $this_sub = (caller 0)[3];
22             Devel::StackTrace->new(frame_filter => sub {
23 0     0     my $caller = shift->{caller};
24 0   0       my $in_nt = $caller->[0] =~ /^WWW::Net::/ || $caller->[3] eq $this_sub;
25 0 0 0       ($seen ||= $in_nt) && !$in_nt || 0;
      0        
26 0           });
27             }
28              
29             sub error {
30 0     0 0   my $self = shift;
31              
32 0 0         return $self->_stringified if $self->_stringified;
33              
34             # Don't walk on $@
35 0           local $@;
36              
37 0   0       my $error = $self->has_fbx_error && $self->fbx_error_text
38             || $self->http_response->status_line;
39              
40 0           my ($location) = $self->stack_trace->frame(0)->as_string =~ /( at .*)/;
41 0   0       return $self->_stringified($error . ($location || ''));
42             }
43              
44             sub fbx_error_text {
45 0     0 0   my $self = shift;
46              
47              
48 0 0         return '' unless $self->has_fbx_error;
49 0           my $e = $self->fbx_error;
50              
51             return try {
52 0     0     exists $e->{msg} && $e->{msg};
53 0   0       } || '';
54             }
55              
56              
57             sub fbx_error_code {
58 0     0 0   my $self = shift;
59              
60             return $self->has_fbx_error
61             && exists $self->fbx_error->{error_code}
62             && $self->fbx_error->{error_code}
63 0   0       || 0;
64             }
65              
66             __PACKAGE__->meta->make_immutable;
67              
68 23     23   8143 no Moose;
  23         36  
  23         145  
69              
70              
71             1;
72             __END__
73              
74             =encoding utf-8
75              
76             =head1 NAME
77              
78             WWW::FBX::Error - Freebox Error Handling
79              
80             =head1 SYNOPSIS
81              
82             use WWW::FBX::Error;
83              
84             =head1 DESCRIPTION
85              
86             WWW::FBX::Error is FBX Error handling
87              
88             =head1 LICENSE
89              
90             Copyright (C) Laurent Kislaire.
91              
92             This library is free software; you can redistribute it and/or modify
93             it under the same terms as Perl itself.
94              
95             =head1 AUTHOR
96              
97             Laurent Kislaire E<lt>teebeenator@gmail.comE<gt>
98              
99             =cut
100