| 1 | package Mojo::WebService::Twitter::Error; | ||||||||||||||||
| 2 | 2 | ▶ | use Mojo::Base -base; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 3 | |||||||||||||||||
| 4 | 2 | ▶ | use Exporter 'import'; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 5 | |||||||||||||||||
| 6 | 0 | ▶ | use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
Subroutine
__ANON__: not called CC=1 SCAR=0.6
Subroutine
__ANON__: not called CC=1 SCAR=0.6
| |||||||||||||||||
| 7 | |||||||||||||||||
| 8 | 2 | our $VERSION = '1.003'; | |||||||||||||||
| 9 | 2 | our @EXPORT_OK = qw(twitter_tx_error); | |||||||||||||||
| 10 | |||||||||||||||||
| 11 | 0 | ▶ | has 'api_errors' => sub { [] }; | ||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||
| 12 | 2 | has ['connection_error','http_status','http_message']; | |||||||||||||||
| 13 | |||||||||||||||||
| 14 | 0 | ▶ | sub twitter_tx_error ($) { __PACKAGE__->new->from_tx(shift) } | ||||||||||||||
|
Subroutine
twitter_tx_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||
| 15 | |||||||||||||||||
| 16 | 0 | ▶ | sub from_tx { | ||||||||||||||
|
Subroutine
from_tx: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||
| 17 | 0 | my ($self, $tx) = @_; | |||||||||||||||
| 18 | 0 | my $res = $tx->res; | |||||||||||||||
| 19 | 0 | delete $self->{connection_error}; | |||||||||||||||
| 20 | 0 | delete $self->{api_errors}; | |||||||||||||||
| 21 | 0 | $self->http_status($res->code); | |||||||||||||||
| 22 | 0 | $self->http_message($res->message); | |||||||||||||||
| 23 | 0 | ▶ | return $self unless my $err = $res->error; | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 24 | 0 | ▶ | $self->connection_error($err->{message}) unless defined $err->{code}; | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 25 | 0 | ▶ | return $self->api_errors(($res->json // {})->{errors} // []); | ||||||||||||||
|
Condition0%0/4
+($res->json // {})->{"errors"} // []
$res->json // {}
MC/DC0%0/1
+($res->json // {})->{"errors"} // []
$res->json
Truth table0%0/2
A
A= $res->json
| |||||||||||||||||
| 26 | } | ||||||||||||||||
| 27 | |||||||||||||||||
| 28 | 0 | ▶ | sub to_string { | ||||||||||||||
|
Subroutine
to_string: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||
| 29 | 0 | my $self = shift; | |||||||||||||||
| 30 | 0 | ▶ | if (defined(my $err = $self->connection_error)) { | ||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||
| 31 | 0 | return "Connection error: $err\n"; | |||||||||||||||
| 32 | 0 | } elsif (my @errs = @{$self->api_errors}) { | |||||||||||||||
| 33 | 0 | return "API error $errs[0]{code}: $errs[0]{message}\n"; | |||||||||||||||
| 34 | } else { | ||||||||||||||||
| 35 | 0 | return 'HTTP status ' . $self->http_status . ': ' . $self->http_message . "\n"; | |||||||||||||||
| 36 | } | ||||||||||||||||
| 37 | } | ||||||||||||||||
| 38 | |||||||||||||||||
| 39 | 2 | 1; | |||||||||||||||
| 40 | |||||||||||||||||
| 41 | =head1 NAME | ||||||||||||||||
| 42 | |||||||||||||||||
| 43 | Mojo::WebService::Twitter::Error - Container for API errors | ||||||||||||||||
| 44 | |||||||||||||||||
| 45 | =head1 SYNOPSIS | ||||||||||||||||
| 46 | |||||||||||||||||
| 47 | my $error = Mojo::WebService::Twitter::Error->new->from_tx($tx); | ||||||||||||||||
| 48 | warn "$_->{code}: $_->{message}\n" for @{$error->api_errors}; | ||||||||||||||||
| 49 | die $error->to_string; | ||||||||||||||||
| 50 | |||||||||||||||||
| 51 | =head1 DESCRIPTION | ||||||||||||||||
| 52 | |||||||||||||||||
| 53 | L<Mojo::WebService::Twitter::Error> is a container for | ||||||||||||||||
| 54 | L<API errors|https://dev.twitter.com/overview/api/response-codes> received from | ||||||||||||||||
| 55 | the Twitter API via L<Mojo::WebService::Twitter>. | ||||||||||||||||
| 56 | |||||||||||||||||
| 57 | =head1 FUNCTIONS | ||||||||||||||||
| 58 | |||||||||||||||||
| 59 | L<Mojo::WebService::Twitter::Error> exports the following functions on demand. | ||||||||||||||||
| 60 | |||||||||||||||||
| 61 | =head2 twitter_tx_error | ||||||||||||||||
| 62 | |||||||||||||||||
| 63 | my $error = twitter_tx_error($tx); | ||||||||||||||||
| 64 | |||||||||||||||||
| 65 | Creates a new L<Mojo::WebService::Twitter::Error> and populates it using | ||||||||||||||||
| 66 | L</"from_tx">. | ||||||||||||||||
| 67 | |||||||||||||||||
| 68 | =head1 ATTRIBUTES | ||||||||||||||||
| 69 | |||||||||||||||||
| 70 | L<Mojo::WebService::Twitter::Error> implements the following attributes. | ||||||||||||||||
| 71 | |||||||||||||||||
| 72 | =head2 api_errors | ||||||||||||||||
| 73 | |||||||||||||||||
| 74 | my $errors = $error->api_errors; | ||||||||||||||||
| 75 | $error = $error->api_errors([{code => 215, message => 'Bad Authentication data.'}]); | ||||||||||||||||
| 76 | |||||||||||||||||
| 77 | Arrayref of error codes and messages received from the Twitter API. | ||||||||||||||||
| 78 | |||||||||||||||||
| 79 | =head2 connection_error | ||||||||||||||||
| 80 | |||||||||||||||||
| 81 | my $message = $error->connection_error; | ||||||||||||||||
| 82 | $error = $error->connection_error('Inactivity timeout'); | ||||||||||||||||
| 83 | |||||||||||||||||
| 84 | Connection error if any. | ||||||||||||||||
| 85 | |||||||||||||||||
| 86 | =head2 http_status | ||||||||||||||||
| 87 | |||||||||||||||||
| 88 | my $status = $error->http_status; | ||||||||||||||||
| 89 | $error = $error->http_status(404); | ||||||||||||||||
| 90 | |||||||||||||||||
| 91 | HTTP status code returned by Twitter API. | ||||||||||||||||
| 92 | |||||||||||||||||
| 93 | =head2 http_message | ||||||||||||||||
| 94 | |||||||||||||||||
| 95 | my $message = $error->http_message; | ||||||||||||||||
| 96 | $error = $error->http_message('Not Found'); | ||||||||||||||||
| 97 | |||||||||||||||||
| 98 | HTTP status message returned by Twitter API. | ||||||||||||||||
| 99 | |||||||||||||||||
| 100 | =head1 METHODS | ||||||||||||||||
| 101 | |||||||||||||||||
| 102 | L<Mojo::WebService::Twitter::Error> inherits all methods from L<Mojo::Base>, | ||||||||||||||||
| 103 | and implements the following new ones. | ||||||||||||||||
| 104 | |||||||||||||||||
| 105 | =head2 from_tx | ||||||||||||||||
| 106 | |||||||||||||||||
| 107 | $error = $error->from_tx($tx); | ||||||||||||||||
| 108 | |||||||||||||||||
| 109 | Load connection, API, and HTTP error data from transaction. | ||||||||||||||||
| 110 | |||||||||||||||||
| 111 | =head2 to_string | ||||||||||||||||
| 112 | |||||||||||||||||
| 113 | my $string = $error->to_string; | ||||||||||||||||
| 114 | |||||||||||||||||
| 115 | String representation of connection, API, or HTTP error. | ||||||||||||||||
| 116 | |||||||||||||||||
| 117 | =head1 OPERATORS | ||||||||||||||||
| 118 | |||||||||||||||||
| 119 | L<Mojo::WebService::Twitter::Error> overloads the following operators. | ||||||||||||||||
| 120 | |||||||||||||||||
| 121 | =head2 bool | ||||||||||||||||
| 122 | |||||||||||||||||
| 123 | Always true. | ||||||||||||||||
| 124 | |||||||||||||||||
| 125 | =head2 stringify | ||||||||||||||||
| 126 | |||||||||||||||||
| 127 | Alias for L</"to_string">. | ||||||||||||||||
| 128 | |||||||||||||||||
| 129 | =head1 BUGS | ||||||||||||||||
| 130 | |||||||||||||||||
| 131 | Report any issues on the public bugtracker. | ||||||||||||||||
| 132 | |||||||||||||||||
| 133 | =head1 AUTHOR | ||||||||||||||||
| 134 | |||||||||||||||||
| 135 | Dan Book <dbook@cpan.org> | ||||||||||||||||
| 136 | |||||||||||||||||
| 137 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||
| 138 | |||||||||||||||||
| 139 | This software is Copyright (c) 2015 by Dan Book. | ||||||||||||||||
| 140 | |||||||||||||||||
| 141 | This is free software, licensed under: | ||||||||||||||||
| 142 | |||||||||||||||||
| 143 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||
| 144 | |||||||||||||||||
| 145 | =head1 SEE ALSO | ||||||||||||||||
| 146 | |||||||||||||||||
| 147 | L<Mojo::WebService::Twitter> | ||||||||||||||||