From 50c355228f7ef4937303a87c01680c90df220cbd Mon Sep 17 00:00:00 2001 From: bernardhanna Date: Wed, 11 Feb 2026 16:32:43 +0000 Subject: [PATCH] Enhance exception handling for Nova requests by logging errors and returning detailed JSON responses when NOVA_SHOW_ERRORS is enabled. Update .env.example to include NOVA_SHOW_ERRORS configuration. --- .env.example | 3 +++ app/Exceptions/Handler.php | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 3b56ae89a..e2e050b16 100644 --- a/.env.example +++ b/.env.example @@ -35,6 +35,9 @@ AWS_URL=https://codeweek-s3.s3.amazonaws.com/ LOG_CHANNEL=stack LOG_STACK=single +# Set to true on live to see Nova form errors in API response (DevTools → Network → failed request → Response) +NOVA_SHOW_ERRORS=false + LOCALES=al,ba,bg,cs,da,de,el,en,es,et,fi,fr,hr,hu,it,lt,lv,me,mk,nl,pl,pt,ro,rs,sk,sl,sv ADMIN_EMAIL=admin@codeweek.test diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index ddb825dab..1d24c7d76 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -3,6 +3,7 @@ namespace App\Exceptions; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer; @@ -30,15 +31,26 @@ class Handler extends ExceptionHandler */ public function report(Throwable $exception) { - // if ($this->shouldReport($exception)) { - // $this->sendEmail($exception); // sends an email - // } + $request = request(); + if ($request && $this->isNovaRequest($request)) { + Log::error('Nova request failed: ' . $exception->getMessage(), [ + 'path' => $request->path(), + 'method' => $request->method(), + 'exception' => $exception->getMessage(), + 'file' => $exception->getFile(), + 'line' => $exception->getLine(), + ]); + } parent::report($exception); } /** * Render an exception into an HTTP response. * + * When NOVA_SHOW_ERRORS=true (e.g. on live for debugging), Nova API errors + * return JSON with the real exception message. Check the failed request in + * DevTools → Network → Response to see "error", "file", "line". + * * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response * @@ -46,9 +58,24 @@ public function report(Throwable $exception) */ public function render($request, Throwable $exception) { + if ($request && $this->isNovaRequest($request) && env('NOVA_SHOW_ERRORS', false)) { + return response()->json([ + 'message' => 'There was a problem submitting the form.', + 'error' => $exception->getMessage(), + 'file' => $exception->getFile(), + 'line' => $exception->getLine(), + ], 500); + } + return parent::render($request, $exception); } + private function isNovaRequest(Request $request): bool + { + $path = $request->path(); + return str_starts_with($path, 'nova/') || str_starts_with($path, 'nova-api/'); + } + /** * Sends an email to the developer about the exception. *