From 76f76f73b42ccfcc23bdc4c6e31cd1bf75f3b70c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Pocrnji=C4=8D?= Date: Wed, 17 Dec 2025 20:45:02 +0100 Subject: [PATCH] error handling importer --- app/Services/ImportProcessor.php | 47 +++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/app/Services/ImportProcessor.php b/app/Services/ImportProcessor.php index 6c6bc24..7b371b7 100644 --- a/app/Services/ImportProcessor.php +++ b/app/Services/ImportProcessor.php @@ -25,6 +25,7 @@ use App\Models\Person\PersonType; use App\Models\Person\PhoneType; use Illuminate\Contracts\Auth\Authenticatable; +use Illuminate\Database\QueryException; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; @@ -1194,7 +1195,8 @@ public function process(Import $import, ?Authenticatable $user = null): array 'user_id' => $user?->getAuthIdentifier(), 'event' => 'processing_failed', 'level' => 'error', - 'message' => $e->getMessage(), + 'message' => $this->safeErrorMessage($e->getMessage()), + 'context' => $this->exceptionContext($e), ]); return ['ok' => false, 'status' => 'failed', 'error' => $e->getMessage()]; @@ -2482,6 +2484,49 @@ private function safeErrorMessage(string $msg): string return $msg; } + /** + * Extract structured exception details for logging. + */ + private function exceptionContext(\Throwable $e): array + { + $ctx = [ + 'exception' => get_class($e), + 'message' => $this->safeErrorMessage($e->getMessage()), + 'code' => $e->getCode(), + 'file' => $e->getFile().':'.$e->getLine(), + ]; + + if (method_exists($e, 'getPrevious') && $e->getPrevious()) { + $prev = $e->getPrevious(); + $ctx['previous'] = [ + 'exception' => get_class($prev), + 'message' => $this->safeErrorMessage($prev->getMessage()), + 'code' => $prev->getCode(), + 'file' => $prev->getFile().':'.$prev->getLine(), + ]; + } + + if ($e instanceof QueryException) { + $ctx['sql'] = $e->getSql(); + $ctx['bindings'] = $e->getBindings(); + $info = $e->errorInfo ?? null; + if (is_array($info)) { + $ctx['sqlstate'] = $info[0] ?? null; + $ctx['driver_error_code'] = $info[1] ?? null; + $ctx['driver_error_message'] = $info[2] ?? null; + } + } elseif (property_exists($e, 'errorInfo')) { + $info = $e->errorInfo; + if (is_array($info)) { + $ctx['sqlstate'] = $info[0] ?? null; + $ctx['driver_error_code'] = $info[1] ?? null; + $ctx['driver_error_message'] = $info[2] ?? null; + } + } + + return $ctx; + } + /** * Build a trimmed raw data preview (first 8 columns, truncated values) for logging. */