Skip to content

Commit 8b580ed

Browse files
authored
Merge pull request #23 from discord-php/improve-exceptions
improve exception classes
2 parents dc1ddcd + 53ce06d commit 8b580ed

8 files changed

+86
-12
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is a part of the DiscordPHP-Http project.
5+
*
6+
* Copyright (c) 2021-present David Cole <[email protected]>
7+
*
8+
* This file is subject to the MIT license that is bundled
9+
* with this source code in the LICENSE file.
10+
*/
11+
12+
namespace Discord\Http\Exceptions;
13+
14+
/**
15+
* Thrown when a request to Discord's REST API returned ClientErrorResponse.
16+
*
17+
* @author SQKo
18+
*/
19+
class BadRequestException extends RequestFailedException
20+
{
21+
protected $code = 400;
22+
}

src/Discord/Exceptions/InvalidTokenException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
*/
1919
class InvalidTokenException extends RequestFailedException
2020
{
21+
protected $code = 401;
2122
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is a part of the DiscordPHP-Http project.
5+
*
6+
* Copyright (c) 2021-present David Cole <[email protected]>
7+
*
8+
* This file is subject to the MIT license that is bundled
9+
* with this source code in the LICENSE file.
10+
*/
11+
12+
namespace Discord\Http\Exceptions;
13+
14+
/**
15+
* Thrown when a request to Discord's REST API method is invalid.
16+
*
17+
* @author SQKo
18+
*/
19+
class MethodNotAllowedException extends RequestFailedException
20+
{
21+
protected $code = 405;
22+
}

src/Discord/Exceptions/NoPermissionsException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
*/
1919
class NoPermissionsException extends RequestFailedException
2020
{
21+
protected $code = 403;
2122
}

src/Discord/Exceptions/NotFoundException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
*/
1919
class NotFoundException extends RequestFailedException
2020
{
21+
protected $code = 404;
2122
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is a part of the DiscordPHP-Http project.
5+
*
6+
* Copyright (c) 2021-present David Cole <[email protected]>
7+
*
8+
* This file is subject to the MIT license that is bundled
9+
* with this source code in the LICENSE file.
10+
*/
11+
12+
namespace Discord\Http\Exceptions;
13+
14+
/**
15+
* Thrown when a request to Discord's REST API got rate limited and the library
16+
* does not know how to handle.
17+
*
18+
* @author SQKo
19+
*/
20+
class RateLimitException extends RequestFailedException
21+
{
22+
protected $code = 429;
23+
}

src/Discord/Exceptions/RequestFailedException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
namespace Discord\Http\Exceptions;
1313

14-
use Exception;
14+
use RuntimeException;
1515

1616
/**
1717
* Thrown when a request to Discord's REST API fails.
1818
*
1919
* @author David Cole <[email protected]>
2020
*/
21-
class RequestFailedException extends Exception
21+
class RequestFailedException extends RuntimeException
2222
{
2323
}

src/Discord/Http.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111

1212
namespace Discord\Http;
1313

14+
use Discord\Http\Exceptions\BadRequestException;
1415
use Discord\Http\Exceptions\ContentTooLongException;
1516
use Discord\Http\Exceptions\InvalidTokenException;
17+
use Discord\Http\Exceptions\MethodNotAllowedException;
1618
use Discord\Http\Exceptions\NoPermissionsException;
1719
use Discord\Http\Exceptions\NotFoundException;
20+
use Discord\Http\Exceptions\RateLimitException;
1821
use Discord\Http\Exceptions\RequestFailedException;
1922
use Discord\Http\Multipart\MultipartBody;
20-
use Exception;
2123
use Psr\Http\Message\ResponseInterface;
2224
use Psr\Log\LoggerInterface;
2325
use React\EventLoop\LoopInterface;
2426
use React\Promise\Deferred;
2527
use React\Promise\ExtendedPromiseInterface;
26-
use RuntimeException;
2728
use SplQueue;
28-
use Throwable;
2929

3030
/**
3131
* Discord HTTP client.
@@ -39,7 +39,7 @@ class Http
3939
*
4040
* @var string
4141
*/
42-
public const VERSION = 'v10.2.3';
42+
public const VERSION = 'v10.3.0';
4343

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

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

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

414414
$deferred->reject($e);
@@ -488,9 +488,9 @@ protected function checkQueue(): void
488488
*
489489
* @param ResponseInterface $response
490490
*
491-
* @return Throwable
491+
* @return \Throwable
492492
*/
493-
public function handleError(ResponseInterface $response): Throwable
493+
public function handleError(ResponseInterface $response): \Throwable
494494
{
495495
$reason = $response->getReasonPhrase().' - ';
496496

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

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

510510
switch ($response->getStatusCode()) {
511+
case 400:
512+
return new BadRequestException($reason, $errorCode);
511513
case 401:
512514
return new InvalidTokenException($reason, $errorCode);
513515
case 403:
514516
return new NoPermissionsException($reason, $errorCode);
515517
case 404:
516518
return new NotFoundException($reason, $errorCode);
519+
case 405:
520+
return new MethodNotAllowedException($reason, $errorCode);
517521
case 500:
518522
if (strpos(strtolower($errorBody), 'longer than 2000 characters') !== false ||
519523
strpos(strtolower($errorBody), 'string value is too long') !== false) {

0 commit comments

Comments
 (0)