Yoast\WP\SEO\MyYoast_Client\Application\Exceptions

Token_Request_Failed_Exception{} │ Yoast 1.0└─ RuntimeException

Exception thrown when a token endpoint request fails.

Wraps OAuth error codes like invalid_grant, invalid_client, invalid_dpop_proof, etc.

Хуков нет.

Использование

$Token_Request_Failed_Exception = new Token_Request_Failed_Exception();
// use class methods

Методы

  1. public __construct( string $error_code, string $error_description = '', int $code = 0, ?Throwable $previous = null )
  2. public static from_response( array $response_body, int $status_code = 0 )
  3. public get_error_code()

Код Token_Request_Failed_Exception{} Yoast 28.9

class Token_Request_Failed_Exception extends RuntimeException {

	/**
	 * The OAuth error code from the token endpoint response.
	 *
	 * @var string
	 */
	private $error_code;

	/**
	 * Token_Request_Failed_Exception constructor.
	 *
	 * @param string         $error_code        The OAuth error code.
	 * @param string         $error_description A human-readable description.
	 * @param int            $code              The HTTP status code.
	 * @param Throwable|null $previous          The previous exception.
	 */
	public function __construct( string $error_code, string $error_description = '', int $code = 0, ?Throwable $previous = null ) {
		$this->error_code = $error_code;

		$message = $error_code;
		if ( $error_description !== '' ) {
			$message .= ': ' . $error_description;
		}

		parent::__construct( $message, $code, $previous );
	}

	/**
	 * Returns the OAuth error code.
	 *
	 * @return string
	 */
	public function get_error_code(): string {
		return $this->error_code;
	}

	/**
	 * Creates an exception from a token endpoint response body.
	 *
	 * @param array<string, string> $response_body The parsed JSON response.
	 * @param int                   $status_code   The HTTP status code.
	 *
	 * @return self
	 */
	public static function from_response( array $response_body, int $status_code = 0 ): self {
		return new self(
			( $response_body['error'] ?? 'unknown_error' ),
			( $response_body['error_description'] ?? '' ),
			$status_code,
		);
	}
}