| 1 | package WWW::FBX::Error; | ||
| 2 | 23 | ▶ | use 5.008001; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 23 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 23 | ▶ | use Try::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 23 | ▶ | use Devel::StackTrace; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | #Stringify using error sub | ||
| 8 | 23 | use overload '""' => \&error, | |
| 9 | 23 | ▶ | 'fallback' => 1; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 23 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 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 | my $seen; | ||
| 21 | my $this_sub = (caller 0)[3]; | ||
| 22 | Devel::StackTrace->new(frame_filter => sub { | ||
| 23 | my $caller = shift->{caller}; | ||
| 24 | my $in_nt = $caller->[0] =~ /^WWW::Net::/ || $caller->[3] eq $this_sub; | ||
| 25 | ($seen ||= $in_nt) && !$in_nt || 0; | ||
| 26 | }); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub error { | ||
| 30 | my $self = shift; | ||
| 31 | |||
| 32 | return $self->_stringified if $self->_stringified; | ||
| 33 | |||
| 34 | # Don't walk on $@ | ||
| 35 | local $@; | ||
| 36 | |||
| 37 | my $error = $self->has_fbx_error && $self->fbx_error_text | ||
| 38 | || $self->http_response->status_line; | ||
| 39 | |||
| 40 | my ($location) = $self->stack_trace->frame(0)->as_string =~ /( at .*)/; | ||
| 41 | return $self->_stringified($error . ($location || '')); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub fbx_error_text { | ||
| 45 | my $self = shift; | ||
| 46 | |||
| 47 | |||
| 48 | return '' unless $self->has_fbx_error; | ||
| 49 | my $e = $self->fbx_error; | ||
| 50 | |||
| 51 | return try { | ||
| 52 | exists $e->{msg} && $e->{msg}; | ||
| 53 | } || ''; | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | sub fbx_error_code { | ||
| 58 | 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; | ||
| 64 | } | ||
| 65 | |||
| 66 | __PACKAGE__->meta->make_immutable; | ||
| 67 | |||
| 68 | no Moose; | ||
| 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 | |||