Skip to content
Merged

Dev #3350

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 30 additions & 3 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -30,25 +31,51 @@ 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
*
* @throws \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.
*
Expand Down
Loading