Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve exception classes #23

Merged
merged 1 commit into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Discord/Exceptions/BadRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is a part of the DiscordPHP-Http project.
*
* Copyright (c) 2021-present David Cole <david.cole1340@gmail.com>
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE file.
*/

namespace Discord\Http\Exceptions;

/**
* Thrown when a request to Discord's REST API returned ClientErrorResponse.
*
* @author SQKo
*/
class BadRequestException extends RequestFailedException
{
protected $code = 400;
}
1 change: 1 addition & 0 deletions src/Discord/Exceptions/InvalidTokenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
*/
class InvalidTokenException extends RequestFailedException
{
protected $code = 401;
}
22 changes: 22 additions & 0 deletions src/Discord/Exceptions/MethodNotAllowedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is a part of the DiscordPHP-Http project.
*
* Copyright (c) 2021-present David Cole <david.cole1340@gmail.com>
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE file.
*/

namespace Discord\Http\Exceptions;

/**
* Thrown when a request to Discord's REST API method is invalid.
*
* @author SQKo
*/
class MethodNotAllowedException extends RequestFailedException
{
protected $code = 405;
}
1 change: 1 addition & 0 deletions src/Discord/Exceptions/NoPermissionsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
*/
class NoPermissionsException extends RequestFailedException
{
protected $code = 403;
}
1 change: 1 addition & 0 deletions src/Discord/Exceptions/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
*/
class NotFoundException extends RequestFailedException
{
protected $code = 404;
}
23 changes: 23 additions & 0 deletions src/Discord/Exceptions/RateLimitException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is a part of the DiscordPHP-Http project.
*
* Copyright (c) 2021-present David Cole <david.cole1340@gmail.com>
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE file.
*/

namespace Discord\Http\Exceptions;

/**
* Thrown when a request to Discord's REST API got rate limited and the library
* does not know how to handle.
*
* @author SQKo
*/
class RateLimitException extends RequestFailedException
{
protected $code = 429;
}
4 changes: 2 additions & 2 deletions src/Discord/Exceptions/RequestFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

namespace Discord\Http\Exceptions;

use Exception;
use RuntimeException;

/**
* Thrown when a request to Discord's REST API fails.
*
* @author David Cole <david.cole1340@gmail.com>
*/
class RequestFailedException extends Exception
class RequestFailedException extends RuntimeException
{
}
24 changes: 14 additions & 10 deletions src/Discord/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@

namespace Discord\Http;

use Discord\Http\Exceptions\BadRequestException;
use Discord\Http\Exceptions\ContentTooLongException;
use Discord\Http\Exceptions\InvalidTokenException;
use Discord\Http\Exceptions\MethodNotAllowedException;
use Discord\Http\Exceptions\NoPermissionsException;
use Discord\Http\Exceptions\NotFoundException;
use Discord\Http\Exceptions\RateLimitException;
use Discord\Http\Exceptions\RequestFailedException;
use Discord\Http\Multipart\MultipartBody;
use Exception;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use React\Promise\ExtendedPromiseInterface;
use RuntimeException;
use SplQueue;
use Throwable;

/**
* Discord HTTP client.
Expand All @@ -39,7 +39,7 @@ class Http
*
* @var string
*/
public const VERSION = 'v10.2.3';
public const VERSION = 'v10.3.0';

/**
* Current Discord HTTP API version.
Expand Down Expand Up @@ -344,7 +344,7 @@ protected function executeRequest(Request $request, Deferred $deferred = null):
} else {
// Some other 429
$this->logger->error($request.' does not contain global rate-limit value');
$rateLimitError = new RuntimeException('No rate limit global response', $statusCode);
$rateLimitError = new RateLimitException('No rate limit global response', $statusCode);
$deferred->reject($rateLimitError);
$request->getDeferred()->reject($rateLimitError);

Expand All @@ -358,7 +358,7 @@ protected function executeRequest(Request $request, Deferred $deferred = null):
} else {
// Some other 429
$this->logger->error($request.' does not contain retry after rate-limit value');
$rateLimitError = new RuntimeException('No rate limit retry after response', $statusCode);
$rateLimitError = new RateLimitException('No rate limit retry after response', $statusCode);
$deferred->reject($rateLimitError);
$request->getDeferred()->reject($rateLimitError);

Expand Down Expand Up @@ -408,7 +408,7 @@ protected function executeRequest(Request $request, Deferred $deferred = null):
$deferred->resolve($response);
$request->getDeferred()->resolve($data);
}
}, function (Exception $e) use ($request, $deferred) {
}, function (\Exception $e) use ($request, $deferred) {
$this->logger->warning($request.' failed: '.$e->getMessage());

$deferred->reject($e);
Expand Down Expand Up @@ -488,9 +488,9 @@ protected function checkQueue(): void
*
* @param ResponseInterface $response
*
* @return Throwable
* @return \Throwable
*/
public function handleError(ResponseInterface $response): Throwable
public function handleError(ResponseInterface $response): \Throwable
{
$reason = $response->getReasonPhrase().' - ';

Expand All @@ -499,7 +499,7 @@ public function handleError(ResponseInterface $response): Throwable

// attempt to prettyify the response content
if (($content = json_decode($errorBody)) !== null) {
if (isset($content->code)) {
if (! empty($content->code)) {
$errorCode = $content->code;
}
$reason .= json_encode($content, JSON_PRETTY_PRINT);
Expand All @@ -508,12 +508,16 @@ public function handleError(ResponseInterface $response): Throwable
}

switch ($response->getStatusCode()) {
case 400:
return new BadRequestException($reason, $errorCode);
case 401:
return new InvalidTokenException($reason, $errorCode);
case 403:
return new NoPermissionsException($reason, $errorCode);
case 404:
return new NotFoundException($reason, $errorCode);
case 405:
return new MethodNotAllowedException($reason, $errorCode);
case 500:
if (strpos(strtolower($errorBody), 'longer than 2000 characters') !== false ||
strpos(strtolower($errorBody), 'string value is too long') !== false) {
Expand Down