fix: manejar excepciones específicas para el padrón estatal y mejorar la validación de respuestas

This commit is contained in:
Juan Felipe Zapata Moreno 2026-02-17 23:12:35 -06:00
parent f04dbccedb
commit aeebda6faa
4 changed files with 37 additions and 8 deletions

View File

@ -0,0 +1,5 @@
<?php
namespace App\Exceptions;
class PadronEstatalException extends \RuntimeException {}

View File

@ -15,6 +15,7 @@
use App\Models\VehicleTagLog; use App\Models\VehicleTagLog;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use App\Exceptions\PadronEstatalException;
use App\Services\RepuveService; use App\Services\RepuveService;
use App\Services\PadronEstatalService; use App\Services\PadronEstatalService;
use App\Jobs\ProcessRepuveResponse; use App\Jobs\ProcessRepuveResponse;
@ -98,6 +99,14 @@ public function vehicleInscription(VehicleStoreRequest $request)
// Obtener NIV para consultar REPUVE Nacional // Obtener NIV para consultar REPUVE Nacional
$niv = $vehicleDataEstatal['niv']; $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 // Consultar REPUVE Nacional para obtener datos oficiales del vehículo
$repuveNacionalData = $this->repuveService->consultarVehiculo($niv, $placa); $repuveNacionalData = $this->repuveService->consultarVehiculo($niv, $placa);
@ -284,6 +293,13 @@ public function vehicleInscription(VehicleStoreRequest $request)
'files' => $uploadedFiles, 'files' => $uploadedFiles,
'total_files' => count($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) { } catch (\Exception $e) {
DB::rollBack(); DB::rollBack();

View File

@ -15,6 +15,7 @@
use App\Models\CatalogNameImg; use App\Models\CatalogNameImg;
use App\Models\VehicleTagLog; use App\Models\VehicleTagLog;
use App\Models\Tag; use App\Models\Tag;
use App\Exceptions\PadronEstatalException;
use App\Services\RepuveService; use App\Services\RepuveService;
use App\Services\PadronEstatalService; use App\Services\PadronEstatalService;
use Exception; use Exception;
@ -205,6 +206,13 @@ public function tagSubstitution(Request $request)
], ],
'record' => $record, '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) { } catch (Exception $e) {
DB::rollBack(); DB::rollBack();

View File

@ -2,7 +2,7 @@
namespace App\Services; namespace App\Services;
use Exception; use App\Exceptions\PadronEstatalException;
class PadronEstatalService class PadronEstatalService
{ {
@ -71,11 +71,11 @@ private function consultarPadron(string $tipo, string $valor): array
$error = curl_error($ch); $error = curl_error($ch);
if ($error) { 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) { 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 // Parsear la respuesta
@ -94,7 +94,7 @@ private function parsearRespuesta(string $soapResponse): array
preg_match('/<result>(.*?)<\/result>/s', $soapResponse, $matches); preg_match('/<result>(.*?)<\/result>/s', $soapResponse, $matches);
if (!isset($matches[1])) { 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]); $jsonContent = trim($matches[1]);
@ -103,24 +103,24 @@ private function parsearRespuesta(string $soapResponse): array
$result = json_decode($jsonContent, true); $result = json_decode($jsonContent, true);
if (json_last_error() !== JSON_ERROR_NONE) { 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 // La respuesta es un array con un objeto que tiene error y datos
if (!isset($result[0])) { 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]; $data = $result[0];
// Verificar si hay error // Verificar si hay error
if ($data['error'] !== 0) { 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 // Verificar si hay datos
if (!isset($data['datos'][0])) { 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]; return $data['datos'][0];