Skip to content

Commit

Permalink
[5.x] Allow custom render callback for overridden exceptions (#11408)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <jason@pixelfear.com>
  • Loading branch information
FrittenKeeZ and jasonvarga authored Feb 5, 2025
1 parent 6e9a16d commit 970ee2b
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Exceptions/Concerns/RendersHttpExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\Exceptions\Concerns;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Statamic\Facades\Cascade;
Expand All @@ -12,8 +13,14 @@

trait RendersHttpExceptions
{
public function render()
private static ?Closure $renderCallback = null;

public function render(Request $request)
{
if (static::$renderCallback && ($response = Closure::fromCallable(static::$renderCallback)->call($this, $request))) {
return $response;
}

if (Statamic::isCpRoute()) {
return response()->view('statamic::errors.'.$this->getStatusCode(), [], $this->getStatusCode());
}
Expand Down Expand Up @@ -82,4 +89,9 @@ private function getCachedError(): ?Response
? $cacher->getCachedPage($request)->toResponse($request)
: null;
}

public static function renderUsing(Closure $callback): void
{
static::$renderCallback = $callback;
}
}

0 comments on commit 970ee2b

Please sign in to comment.