ldc-arcadia-genesis-php-client/src/Exception/ApiErrorException.php

41 lines
1.2 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Uwil\ArcadiaApiPhpClient\Exception;
use Uwil\ArcadiaApiPhpClient\Enum\ArcadiaGenesisApiErrorCode;
class ApiErrorException extends \DomainException implements ArcadiaGenesisApiException
{
public readonly ArcadiaGenesisApiErrorCode $apiError;
public function __construct(ArcadiaGenesisApiErrorCode $apiError, ?\Throwable $previous = null)
{
$this->apiError = $apiError;
parent::__construct(
$apiError->description(),
$apiError->httpStatusCode(),
$previous
);
}
/**
* @param array<string,mixed> $error
*/
public static function fromResponseError(array $error): self
{
// FIXME: Patch degueu en attendant une correction de leur part.
// * Si l'api retourne de multiples erreurs, ce qu'elle n'est pas sensée faire alors on prend la derniere du tableau.
if (count($error) > 1 && is_int(array_keys($error)[0] ?? null)) {
$error = array_pop($error);
}
$code = ArcadiaGenesisApiErrorCode::tryFrom($error['code']) ?? ArcadiaGenesisApiErrorCode::UNKNOWN;
return new self($code);
}
}