Implement robust error handling and configuration for Logistics API interactions

- Introduced `LogisticsApiException` to handle connection and request errors with user-friendly messages in French.
- Updated `LogisticsService` to include configurable timeout, connection timeout, retry attempts, and sleep duration for retries.
- Enhanced error handling in Filament pages to catch `LogisticsApiException` and provide clear feedback to users.
- Updated `.env` and `config/logistics.php` to support new configuration options.
- Added logging for failed API requests in `api_request_logs`.
- Created comprehensive API documentation for Logistics endpoints.
This commit is contained in:
2026-02-20 10:06:04 +01:00
parent 07a3b3a874
commit 4aef33f270
18 changed files with 820 additions and 64 deletions

View File

@@ -2,6 +2,8 @@
namespace App\Services;
use App\Exceptions\LogisticsApiException;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
@@ -14,11 +16,23 @@ class LogisticsService
private string $folder;
private int $timeout;
private int $connectTimeout;
private int $retryTimes;
private int $retrySleepMs;
public function __construct()
{
$this->baseUrl = rtrim(config('logistics.base_url'), '/');
$this->apiKey = config('logistics.api_key');
$this->folder = config('logistics.folder');
$this->timeout = config('logistics.timeout', 30);
$this->connectTimeout = config('logistics.connect_timeout', 10);
$this->retryTimes = config('logistics.retry.times', 3);
$this->retrySleepMs = config('logistics.retry.sleep_ms', 500);
}
public function tablesList(): array
@@ -141,31 +155,62 @@ class LogisticsService
/**
* @return array{data: mixed, metadata: array, error: mixed}
*
* @throws LogisticsApiException
*/
private function post(string $endpoint, array $params = []): array
{
$url = "{$this->baseUrl}/{$this->folder}/{$endpoint}";
$response = Http::withHeaders([
'X-API-KEY' => $this->apiKey,
])->post($url, $params);
try {
$response = Http::withHeaders([
'X-API-KEY' => $this->apiKey,
])
->timeout($this->timeout)
->connectTimeout($this->connectTimeout)
->retry($this->retryTimes, $this->retrySleepMs, fn (\Exception $e) => $e instanceof ConnectionException)
->post($url, $params);
$this->logRequest($endpoint, $params, $response);
$this->logRequest($endpoint, $params, $response);
return $response->json() ?? [
'data' => null,
'metadata' => ['rowcount' => 0, 'issuccess' => false],
'error' => 'Empty response from API',
];
return $response->json() ?? [
'data' => null,
'metadata' => ['rowcount' => 0, 'issuccess' => false],
'error' => 'Empty response from API',
];
} catch (ConnectionException $e) {
$this->logFailedRequest($endpoint, $params, $e);
throw LogisticsApiException::connectionTimeout($endpoint, $params, $e);
} catch (\Throwable $e) {
$this->logFailedRequest($endpoint, $params, $e);
throw LogisticsApiException::requestFailed($endpoint, $params, $e);
}
}
private function logRequest(string $endpoint, array $params, Response $response): void
{
$body = $response->body();
$isValidJson = json_decode($body) !== null || $body === 'null';
DB::table('api_request_logs')->insert([
'endpoint' => $endpoint,
'parameters' => json_encode($params),
'response_status' => $response->status(),
'response_data' => $response->body(),
'response_data' => $isValidJson ? $body : json_encode(['raw' => $body]),
'created_at' => now(),
'updated_at' => now(),
]);
}
private function logFailedRequest(string $endpoint, array $params, \Throwable $exception): void
{
DB::table('api_request_logs')->insert([
'endpoint' => $endpoint,
'parameters' => json_encode($params),
'response_status' => 0,
'response_data' => json_encode(['error' => $exception->getMessage()]),
'created_at' => now(),
'updated_at' => now(),
]);