Skip to content

Commit 02b2f30

Browse files
committed
add Discord Error code
1 parent 77e1031 commit 02b2f30

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/Discord/Http.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -470,28 +470,32 @@ public function handleError(ResponseInterface $response): Throwable
470470
{
471471
$reason = $response->getReasonPhrase().' - ';
472472

473+
$errorBody = (string) $response->getBody();
474+
$errorCode = 0;
475+
473476
// attempt to prettyify the response content
474-
if (($content = json_decode((string) $response->getBody())) !== null) {
477+
if (($content = json_decode($errorBody)) !== null) {
478+
$errorCode = $content->code ?? 0;
475479
$reason .= json_encode($content, JSON_PRETTY_PRINT);
476480
} else {
477-
$reason .= (string) $response->getBody();
481+
$reason .= $errorBody;
478482
}
479483

480484
switch ($response->getStatusCode()) {
481485
case 401:
482-
return new InvalidTokenException($reason);
486+
return new InvalidTokenException($reason, $errorCode);
483487
case 403:
484-
return new NoPermissionsException($reason);
488+
return new NoPermissionsException($reason, $errorCode);
485489
case 404:
486-
return new NotFoundException($reason);
490+
return new NotFoundException($reason, $errorCode);
487491
case 500:
488-
if (strpos(strtolower((string) $response->getBody()), 'longer than 2000 characters') !== false ||
489-
strpos(strtolower((string) $response->getBody()), 'string value is too long') !== false) {
492+
if (strpos(strtolower($errorBody), 'longer than 2000 characters') !== false ||
493+
strpos(strtolower($errorBody), 'string value is too long') !== false) {
490494
// Response was longer than 2000 characters and was blocked by Discord.
491495
return new ContentTooLongException('Response was more than 2000 characters. Use another method to get this data.');
492496
}
493497
default:
494-
return new RequestFailedException($reason);
498+
return new RequestFailedException($reason, $errorCode);
495499
}
496500
}
497501

0 commit comments

Comments
 (0)