fix: manejar excepciones específicas para el padrón estatal y mejorar la validación de respuestas
This commit is contained in:
parent
f04dbccedb
commit
aeebda6faa
5
app/Exceptions/PadronEstatalException.php
Normal file
5
app/Exceptions/PadronEstatalException.php
Normal file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
class PadronEstatalException extends \RuntimeException {}
|
||||
@ -15,6 +15,7 @@
|
||||
use App\Models\VehicleTagLog;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Exceptions\PadronEstatalException;
|
||||
use App\Services\RepuveService;
|
||||
use App\Services\PadronEstatalService;
|
||||
use App\Jobs\ProcessRepuveResponse;
|
||||
@ -98,6 +99,14 @@ public function vehicleInscription(VehicleStoreRequest $request)
|
||||
// Obtener NIV para consultar REPUVE Nacional
|
||||
$niv = $vehicleDataEstatal['niv'];
|
||||
|
||||
if (empty($niv)) {
|
||||
DB::rollBack();
|
||||
return ApiResponse::BAD_REQUEST->response([
|
||||
'message' => 'El padrón estatal no retornó un NIV válido para la placa proporcionada.',
|
||||
'placa' => $placa,
|
||||
]);
|
||||
}
|
||||
|
||||
// Consultar REPUVE Nacional para obtener datos oficiales del vehículo
|
||||
$repuveNacionalData = $this->repuveService->consultarVehiculo($niv, $placa);
|
||||
|
||||
@ -284,6 +293,13 @@ public function vehicleInscription(VehicleStoreRequest $request)
|
||||
'files' => $uploadedFiles,
|
||||
'total_files' => count($uploadedFiles),
|
||||
]);
|
||||
} catch (PadronEstatalException $e) {
|
||||
DB::rollBack();
|
||||
|
||||
return ApiResponse::INTERNAL_ERROR->response([
|
||||
'message' => 'Error al consultar el padrón estatal.',
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
use App\Models\CatalogNameImg;
|
||||
use App\Models\VehicleTagLog;
|
||||
use App\Models\Tag;
|
||||
use App\Exceptions\PadronEstatalException;
|
||||
use App\Services\RepuveService;
|
||||
use App\Services\PadronEstatalService;
|
||||
use Exception;
|
||||
@ -205,6 +206,13 @@ public function tagSubstitution(Request $request)
|
||||
],
|
||||
'record' => $record,
|
||||
]);
|
||||
} catch (PadronEstatalException $e) {
|
||||
DB::rollBack();
|
||||
|
||||
return ApiResponse::INTERNAL_ERROR->response([
|
||||
'message' => 'Error al consultar el padrón estatal.',
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
DB::rollBack();
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Exception;
|
||||
use App\Exceptions\PadronEstatalException;
|
||||
|
||||
class PadronEstatalService
|
||||
{
|
||||
@ -71,11 +71,11 @@ private function consultarPadron(string $tipo, string $valor): array
|
||||
$error = curl_error($ch);
|
||||
|
||||
if ($error) {
|
||||
throw new Exception("Error en la petición al padrón estatal: {$error}");
|
||||
throw new PadronEstatalException("Error en la petición al padrón estatal: {$error}");
|
||||
}
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
throw new Exception("Error HTTP {$httpCode} al consultar padrón estatal");
|
||||
throw new PadronEstatalException("Error HTTP {$httpCode} al consultar padrón estatal");
|
||||
}
|
||||
|
||||
// Parsear la respuesta
|
||||
@ -94,7 +94,7 @@ private function parsearRespuesta(string $soapResponse): array
|
||||
preg_match('/<result>(.*?)<\/result>/s', $soapResponse, $matches);
|
||||
|
||||
if (!isset($matches[1])) {
|
||||
throw new Exception("No se pudo extraer el resultado del padrón estatal");
|
||||
throw new PadronEstatalException("No se pudo extraer el resultado del padrón estatal");
|
||||
}
|
||||
|
||||
$jsonContent = trim($matches[1]);
|
||||
@ -103,24 +103,24 @@ private function parsearRespuesta(string $soapResponse): array
|
||||
$result = json_decode($jsonContent, true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
throw new Exception("Error al decodificar JSON del padrón estatal: " . json_last_error_msg());
|
||||
throw new PadronEstatalException("Error al decodificar JSON del padrón estatal: " . json_last_error_msg());
|
||||
}
|
||||
|
||||
// La respuesta es un array con un objeto que tiene error y datos
|
||||
if (!isset($result[0])) {
|
||||
throw new Exception("Formato de respuesta inesperado del padrón estatal");
|
||||
throw new PadronEstatalException("Formato de respuesta inesperado del padrón estatal");
|
||||
}
|
||||
|
||||
$data = $result[0];
|
||||
|
||||
// Verificar si hay error
|
||||
if ($data['error'] !== 0) {
|
||||
throw new Exception("Error en consulta al padrón estatal: código {$data['error']}");
|
||||
throw new PadronEstatalException("Error en consulta al padrón estatal: código {$data['error']}");
|
||||
}
|
||||
|
||||
// Verificar si hay datos
|
||||
if (!isset($data['datos'][0])) {
|
||||
throw new Exception("No se encontraron datos del vehículo en el padrón estatal");
|
||||
throw new PadronEstatalException("No se encontraron datos del vehículo en el padrón estatal");
|
||||
}
|
||||
|
||||
return $data['datos'][0];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user