manejo de errores
This commit is contained in:
parent
78ac5ab75e
commit
c7f1b46714
@ -200,12 +200,25 @@ public function vehicleInscription(VehicleStoreRequest $request)
|
||||
$error = Error::where('code', $apiResponse['error_code'])->first();
|
||||
|
||||
if (!$error) {
|
||||
DB::rollBack();
|
||||
return ApiResponse::BAD_REQUEST->response([
|
||||
'message' => 'Código de error no encontrado en el catálogo',
|
||||
// Usar error genérico si no se encuentra el código
|
||||
logger()->warning('Código de error REPUVE no catalogado', [
|
||||
'error_code' => $apiResponse['error_code'],
|
||||
'error_message' => $apiResponse['error_message'],
|
||||
'niv' => $niv,
|
||||
]);
|
||||
|
||||
// Buscar error genérico -1 (Error Interno)
|
||||
$error = Error::where('code', '-1')->first();
|
||||
|
||||
if (!$error) {
|
||||
// Si ni siquiera existe el error genérico, crear uno temporal
|
||||
$error = Error::create([
|
||||
'code' => '-1',
|
||||
'name' => 'Error Interno',
|
||||
'description' => 'Error interno del web service. Problemas con la base de datos o en la red.',
|
||||
'type' => 'Error de Conexión',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Guardar error en Record
|
||||
|
||||
@ -356,12 +356,25 @@ public function vehicleUpdate(VehicleUpdateRequest $request)
|
||||
$error = Error::where('code', $apiResponse['error_code'])->first();
|
||||
|
||||
if (!$error) {
|
||||
DB::rollBack();
|
||||
return ApiResponse::INTERNAL_ERROR->response([
|
||||
'message' => 'Código de error no encontrado en el catálogo',
|
||||
// Usar error genérico si no se encuentra el código
|
||||
logger()->warning('Código de error REPUVE no catalogado', [
|
||||
'error_code' => $apiResponse['error_code'],
|
||||
'error_message' => $apiResponse['error_message'],
|
||||
'niv' => $niv,
|
||||
]);
|
||||
|
||||
// Buscar error
|
||||
$error = Error::where('code', '-1')->first();
|
||||
|
||||
if (!$error) {
|
||||
// Si ni siquiera existe el error genérico
|
||||
$error = Error::create([
|
||||
'code' => '-1',
|
||||
'name' => 'Error Interno',
|
||||
'description' => 'Error interno del web service. Problemas con la base de datos o en la red.',
|
||||
'type' => 'Error de Conexión',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$record->update([
|
||||
|
||||
@ -11,7 +11,9 @@ class Error extends Model
|
||||
|
||||
protected $fillable = [
|
||||
'code',
|
||||
'name',
|
||||
'description',
|
||||
'type'
|
||||
];
|
||||
|
||||
public function records()
|
||||
|
||||
@ -72,7 +72,14 @@ private function parseVehicleResponse(string $soapResponse, string $niv)
|
||||
preg_match('/<return>(.*?)<\/return>/s', $soapResponse, $matches);
|
||||
|
||||
if (!isset($matches[1])) {
|
||||
throw new Exception("No se pudo extraer la respuesta del SOAP");
|
||||
return [
|
||||
'has_error' => true,
|
||||
'error_code' => '108',
|
||||
'error_message' => 'El resultado que devuelve el Web service es de un formato distinto al esperado',
|
||||
'timestamp' => now()->toDateTimeString(),
|
||||
'niv' => $niv,
|
||||
'repuve_response' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$contenido = trim($matches[1]);
|
||||
@ -80,10 +87,18 @@ private function parseVehicleResponse(string $soapResponse, string $niv)
|
||||
// Verificar si hay error
|
||||
if (str_starts_with($contenido, 'ERROR:')) {
|
||||
$errorMessage = str_replace('ERROR:', '', $contenido);
|
||||
|
||||
// Intentar extraer código de error del mensaje (formato: "ERROR:461|Mensaje")
|
||||
$errorCode = '-1'; // Por defecto: Error Interno
|
||||
if (preg_match('/^(\d+)\|/', $errorMessage, $codeMatch)) {
|
||||
$errorCode = $codeMatch[1];
|
||||
$errorMessage = substr($errorMessage, strlen($errorCode) + 1);
|
||||
}
|
||||
|
||||
return [
|
||||
'has_error' => true,
|
||||
'error_code' => 'REPUVE_ERROR',
|
||||
'error_message' => $errorMessage,
|
||||
'error_code' => $errorCode,
|
||||
'error_message' => trim($errorMessage),
|
||||
'timestamp' => now()->toDateTimeString(),
|
||||
'niv' => $niv,
|
||||
'repuve_response' => null,
|
||||
@ -139,7 +154,14 @@ private function parseVehicleResponse(string $soapResponse, string $niv)
|
||||
];
|
||||
}
|
||||
|
||||
throw new Exception("Formato de respuesta desconocido: {$contenido}");
|
||||
return [
|
||||
'has_error' => true,
|
||||
'error_code' => '108',
|
||||
'error_message' => 'El resultado que devuelve el Web service es de un formato distinto al esperado',
|
||||
'timestamp' => now()->toDateTimeString(),
|
||||
'niv' => $niv,
|
||||
'repuve_response' => null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -255,8 +277,8 @@ public function inscribirVehiculo(array $datos)
|
||||
if ($curlError) {
|
||||
return [
|
||||
'has_error' => true,
|
||||
'error_code' => 'CURL_ERROR',
|
||||
'error_message' => "Error cURL: {$curlError}",
|
||||
'error_code' => '103',
|
||||
'error_message' => "Error de conexión al Web Service desde el cliente: {$curlError}",
|
||||
'timestamp' => now()->toDateTimeString(),
|
||||
'http_code' => $httpCode,
|
||||
'raw_response' => $response,
|
||||
@ -266,8 +288,8 @@ public function inscribirVehiculo(array $datos)
|
||||
if ($httpCode !== 200) {
|
||||
return [
|
||||
'has_error' => true,
|
||||
'error_code' => 'HTTP_ERROR',
|
||||
'error_message' => "Error HTTP {$httpCode}",
|
||||
'error_code' => '-1',
|
||||
'error_message' => "Error interno del web service. Código HTTP {$httpCode}",
|
||||
'timestamp' => now()->toDateTimeString(),
|
||||
'http_code' => $httpCode,
|
||||
'raw_response' => $response,
|
||||
@ -283,33 +305,61 @@ public function inscribirVehiculo(array $datos)
|
||||
*/
|
||||
private function parsearRespuestaInscripcion(string $soapResponse)
|
||||
{
|
||||
// Retornar la respuesta completa
|
||||
preg_match('/<return>(.*?)<\/return>/s', $soapResponse, $matches);
|
||||
|
||||
if (!isset($matches[1])) {
|
||||
return [
|
||||
'has_error' => true,
|
||||
'error_code' => '108',
|
||||
'error_message' => 'El resultado que devuelve el Web service es de un formato distinto al esperado',
|
||||
'timestamp' => now()->toDateTimeString(),
|
||||
'raw_response' => $soapResponse,
|
||||
'repuve_response' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$contenido = trim($matches[1]);
|
||||
|
||||
if (str_starts_with($contenido, 'ERROR:')) {
|
||||
$errorMessage = str_replace('ERROR:', '', $contenido);
|
||||
|
||||
$errorCode = '-1';
|
||||
if (preg_match('/^(\d+)\|/', $errorMessage, $codeMatch)) {
|
||||
$errorCode = $codeMatch[1];
|
||||
$errorMessage = substr($errorMessage, strlen($errorCode) + 1);
|
||||
}
|
||||
|
||||
return [
|
||||
'has_error' => true,
|
||||
'error_code' => $errorCode,
|
||||
'error_message' => trim($errorMessage),
|
||||
'timestamp' => now()->toDateTimeString(),
|
||||
'raw_response' => $soapResponse,
|
||||
'repuve_response' => null,
|
||||
];
|
||||
} elseif (str_starts_with($contenido, 'OK:')) {
|
||||
$datos = str_replace('OK:', '', $contenido);
|
||||
|
||||
return [
|
||||
'has_error' => false,
|
||||
'error_code' => null,
|
||||
'error_message' => null,
|
||||
'timestamp' => now()->toDateTimeString(),
|
||||
'raw_response' => $soapResponse,
|
||||
'parsed_response' => $this->extraerContenidoSOAP($soapResponse),
|
||||
'repuve_response' => [
|
||||
'status' => 'OK',
|
||||
'data' => $datos,
|
||||
],
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'has_error' => true,
|
||||
'error_code' => '108',
|
||||
'error_message' => 'El resultado que devuelve el Web service es de un formato distinto al esperado',
|
||||
'timestamp' => now()->toDateTimeString(),
|
||||
'raw_response' => $soapResponse,
|
||||
'repuve_response' => null,
|
||||
];
|
||||
}
|
||||
|
||||
private function extraerContenidoSOAP(string $soapResponse)
|
||||
{
|
||||
// Intentar extraer el contenido del tag <return>
|
||||
preg_match('/<return>(.*?)<\/return>/s', $soapResponse, $matches);
|
||||
|
||||
if (isset($matches[1])) {
|
||||
return trim($matches[1]);
|
||||
}
|
||||
|
||||
// Si no hay tag <return>, intentar con otros tags comunes
|
||||
preg_match('/<result>(.*?)<\/result>/s', $soapResponse, $matches);
|
||||
|
||||
if (isset($matches[1])) {
|
||||
return trim($matches[1]);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,5 +23,10 @@ public function run(): void
|
||||
$this->call(RoleSeeder::class);
|
||||
$this->call(UserSeeder::class);
|
||||
$this->call(SettingSeeder::class);
|
||||
$this->call(ErrorSeeder::class);
|
||||
$this->call(CatalogNameImgSeeder::class);
|
||||
$this->call(MunicipalitySeeder::class);
|
||||
$this->call(ModuleSeeder::class);
|
||||
$this->call(CatalogTagStatusSeeder::class);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user