1
0
Fork 0
mirror of synced 2024-09-30 01:08:13 +13:00

Create OAuth2 exception class

This commit is contained in:
Steven Nguyen 2023-07-14 17:23:17 -07:00
parent 7de29e2991
commit 3499a7028c
No known key found for this signature in database
3 changed files with 69 additions and 0 deletions

View file

@ -195,6 +195,21 @@ return [
'description' => 'Missing ID from OAuth2 provider.',
'code' => 400,
],
Exception::USER_OAUTH2_BAD_REQUEST => [
'name' => Exception::USER_OAUTH2_BAD_REQUEST,
'description' => 'OAuth2 provider rejected the bad request.',
'code' => 400,
],
Exception::USER_OAUTH2_UNAUTHORIZED => [
'name' => Exception::USER_OAUTH2_UNAUTHORIZED,
'description' => 'OAuth2 provider rejected the unauthorized request.',
'code' => 401,
],
Exception::USER_OAUTH2_PROVIDER_ERROR => [
'name' => Exception::USER_OAUTH2_PROVIDER_ERROR,
'description' => 'OAuth2 provider returned some error.',
'code' => 424,
],
/** Teams */
Exception::TEAM_NOT_FOUND => [

View file

@ -0,0 +1,51 @@
<?php
namespace Appwrite\Auth\OAuth2;
use Appwrite\Extend\Exception as AppwriteException;
class Exception extends AppwriteException
{
protected string $response = '';
protected string $error = '';
protected string $errorDescription = '';
public function __construct(string $response = '', int $code = 0, \Throwable $previous = null)
{
$this->response = $response;
$this->message = $response;
$decoded = json_decode($response, true);
if (\is_array($decoded)) {
$this->error = $decoded['error'];
$this->errorDescription = $decoded['error_description'];
$this->message = $this->error . ': ' . $this->errorDescription;
}
$type = match ($code) {
400 => AppwriteException::USER_OAUTH2_BAD_REQUEST,
401 => AppwriteException::USER_OAUTH2_UNAUTHORIZED,
default => AppwriteException::USER_OAUTH2_PROVIDER_ERROR
};
parent::__construct($type, $this->message, $code, $previous);
}
/**
* Get the error parameter from the response.
*
* See https://datatracker.ietf.org/doc/html/rfc6749#section-5.2 for more information.
*/
public function getError(): string
{
return $this->error;
}
/**
* Get the error_description parameter from the response.
*
* See https://datatracker.ietf.org/doc/html/rfc6749#section-5.2 for more information.
*/
public function getErrorDescription(): string
{
return $this->errorDescription;
}
}

View file

@ -75,6 +75,9 @@ class Exception extends \Exception
public const USER_PHONE_ALREADY_EXISTS = 'user_phone_already_exists';
public const USER_PHONE_NOT_FOUND = 'user_phone_not_found';
public const USER_MISSING_ID = 'user_missing_id';
public const USER_OAUTH2_BAD_REQUEST = 'user_oauth2_bad_request';
public const USER_OAUTH2_UNAUTHORIZED = 'user_oauth2_unauthorized';
public const USER_OAUTH2_PROVIDER_ERROR = 'user_oauth2_provider_error';
/** Teams */
public const TEAM_NOT_FOUND = 'team_not_found';