Skip to content

Commit

Permalink
handle unauthorized error
Browse files Browse the repository at this point in the history
  • Loading branch information
felixdorn authored Sep 11, 2022
1 parent 60e309b commit c5a7f3f
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/Exceptions/TwitterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ public static function fromResponse(ResponseInterface $response): TwitterExcepti
{
$decoded = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);

if (array_key_exists('status', $decoded) && $decoded['status'] === 429) {
$reset = implode('', $response->getHeader('x-rate-limit-reset'));

if ($reset == '') {
$reset = 'unknown';
}

return new self('Too many requests (reset in: ' . $reset . ').', $decoded);
if (array_key_exists('status', $decoded)) {
return (match($decoded['status']) {
429 => function () use ($response) {
$reset = implode('', $response->getHeader('x-rate-limit-reset'));

if ($reset == '') {
$reset = 'unknown';
}

return new self('Too many requests (reset in: ' . $reset . ').', $decoded);
},
401 => fn () => new self('Unauthorized.', $decoded)
})();
}

/* @phpstan-ignore-next-line */
Expand Down

0 comments on commit c5a7f3f

Please sign in to comment.