36 lines
2.2 KiB
PHP
36 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Serp\Providers\DataForSeo;
|
|
|
|
use App\Serp\Exceptions\PermanentProviderException;
|
|
use App\Serp\Exceptions\ProviderAuthenticationException;
|
|
use App\Serp\Exceptions\ProviderConfigurationException;
|
|
use App\Serp\Exceptions\ProviderRateLimitException;
|
|
use App\Serp\Exceptions\ProviderTimeoutException;
|
|
use App\Serp\Exceptions\SerpProviderException;
|
|
use App\Serp\Exceptions\TransientProviderException;
|
|
use App\Serp\Exceptions\UnexpectedProviderResponseException;
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
|
|
final class DataForSeoErrorClassifier
|
|
{
|
|
public function connection(ConnectionException $e): SerpProviderException
|
|
{
|
|
return str_contains(strtolower($e->getMessage()), 'timed out') ? new ProviderTimeoutException('DataForSEO request timed out.', previous: $e) : new TransientProviderException('DataForSEO connection failed.', previous: $e);
|
|
}
|
|
|
|
public function http(int $s): SerpProviderException
|
|
{
|
|
return match (true) {
|
|
$s === 401 || $s === 403 => new ProviderAuthenticationException('DataForSEO authentication failed.'),$s === 429 => new ProviderRateLimitException('DataForSEO HTTP rate limit.'),$s >= 500 => new TransientProviderException('DataForSEO server error.'),default => new PermanentProviderException('DataForSEO HTTP request rejected.')
|
|
};
|
|
}
|
|
|
|
public function api(int $s): SerpProviderException
|
|
{
|
|
return match (true) {
|
|
$s === 40100 || $s === 40104 => new ProviderAuthenticationException('DataForSEO account is unavailable.'),$s === 40200 || $s === 40201 => new ProviderConfigurationException('DataForSEO billing is unavailable.'),in_array($s, [40101, 40103, 50301, 50303, 50304], true) => new TransientProviderException('DataForSEO temporary error.'),in_array($s, [50401, 50402], true) => new ProviderTimeoutException('DataForSEO processing timed out.'),$s === 42900 => new ProviderRateLimitException('DataForSEO rate limit.'),$s >= 40000 && $s < 50000 => new PermanentProviderException('DataForSEO rejected the task.'),$s >= 50000 => new TransientProviderException('DataForSEO service error.'),default => new UnexpectedProviderResponseException('Unexpected DataForSEO status code.')
|
|
};
|
|
}
|
|
}
|