| blib/lib/Mojo/WebService/Twitter/Error.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 9 | 27 | 33.3 |
| branch | 0 | 8 | 0.0 |
| condition | 0 | 4 | 0.0 |
| subroutine | 3 | 7 | 42.8 |
| pod | 3 | 3 | 100.0 |
| total | 15 | 49 | 30.6 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package Mojo::WebService::Twitter::Error; | ||||||
| 2 | 2 | 2 | 14 | use Mojo::Base -base; | |||
| 2 | 6 | ||||||
| 2 | 12 | ||||||
| 3 | |||||||
| 4 | 2 | 2 | 291 | use Exporter 'import'; | |||
| 2 | 23 | ||||||
| 2 | 190 | ||||||
| 5 | |||||||
| 6 | 2 | 2 | 14 | use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1; | |||
| 2 | 0 | 4 | |||||
| 2 | 23 | ||||||
| 0 | |||||||
| 0 | |||||||
| 7 | |||||||
| 8 | our $VERSION = '1.001'; | ||||||
| 9 | our @EXPORT_OK = qw(twitter_tx_error); | ||||||
| 10 | |||||||
| 11 | has 'api_errors' => sub { [] }; | ||||||
| 12 | has ['connection_error','http_status','http_message']; | ||||||
| 13 | |||||||
| 14 | 0 | 0 | 1 | sub twitter_tx_error ($) { __PACKAGE__->new->from_tx(shift) } | |||
| 15 | |||||||
| 16 | sub from_tx { | ||||||
| 17 | 0 | 0 | 1 | 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 | 0 | return $self unless my $err = $res->error; | ||||
| 24 | 0 | 0 | $self->connection_error($err->{message}) unless defined $err->{code}; | ||||
| 25 | 0 | 0 | return $self->api_errors(($res->json // {})->{errors} // []); | ||||
| 0 | |||||||
| 26 | } | ||||||
| 27 | |||||||
| 28 | sub to_string { | ||||||
| 29 | 0 | 0 | 1 | my $self = shift; | |||
| 30 | 0 | 0 | if (defined(my $err = $self->connection_error)) { | ||||
| 0 | |||||||
| 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 | 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 |
||||||
| 54 | L |
||||||
| 55 | the Twitter API via L |
||||||
| 56 | |||||||
| 57 | =head1 FUNCTIONS | ||||||
| 58 | |||||||
| 59 | L |
||||||
| 60 | |||||||
| 61 | =head2 twitter_tx_error | ||||||
| 62 | |||||||
| 63 | my $error = twitter_tx_error($tx); | ||||||
| 64 | |||||||
| 65 | Creates a new L |
||||||
| 66 | L"from_tx">. | ||||||
| 67 | |||||||
| 68 | =head1 ATTRIBUTES | ||||||
| 69 | |||||||
| 70 | L |
||||||
| 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 |
||||||
| 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 |
||||||
| 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 |
||||||
| 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 |