Skip to content

Commit

Permalink
Unify exception renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
seriquynh committed Aug 6, 2024
1 parent 7e69645 commit 4a030d9
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
8 changes: 2 additions & 6 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -837,12 +837,8 @@ protected function convertExceptionToResponse(Throwable $e)
protected function renderExceptionContent(Throwable $e)
{
try {
if (config('app.debug')) {
if (app()->has(ExceptionRenderer::class)) {
return $this->renderExceptionWithCustomRenderer($e);
} elseif ($this->container->bound(Renderer::class)) {
return $this->container->make(Renderer::class)->render(request(), $e);
}
if (config('app.debug') && app()->has(ExceptionRenderer::class)) {
return $this->renderExceptionWithCustomRenderer($e);
}

return $this->renderExceptionWithSymfony($e, config('app.debug'));
Expand Down
69 changes: 69 additions & 0 deletions src/Illuminate/Foundation/Exceptions/Renderer/LaravelRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Illuminate\Foundation\Exceptions\Renderer;

use Illuminate\Contracts\Foundation\ExceptionRenderer;
use Illuminate\Http\Request;

class LaravelRenderer implements ExceptionRenderer
{
/**
* The renderer instance.
*
* @var \Illuminate\Foundation\Exceptions\Renderer\Renderer $renderer
*/
protected $renderer;

/**
* The request instance.
*
* @var \Illuminate\Http\Request $renderer
*/
protected $request;

/**
* Create a new default Laravel exception renderer instance.
*
* @param \Illuminate\Foundation\Exceptions\Renderer\Renderer $renderer
* @param \Illuminate\Http\Request $request
*/
public function __construct(Renderer $renderer, Request $request)
{
$this->renderer = $renderer;
$this->request = $request;
}

/**
* Renders the given exception as HTML.
*
* @param \Throwable $throwable
* @return string
*/
public function render($throwable)
{
return $this->renderer->render($this->request, $throwable);
}

/**
* Get the request instance.
*
* @return \Illuminate\Http\Request
*/
public function getRequest()
{
return $this->request;
}

/**
* Set the request instance.
*
* @param \Illuminate\Http\Request $request
* @return $this
*/
public function setRequest(Request $request)
{
$this->request = $request;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Foundation\ExceptionRenderer;
use Illuminate\Contracts\Foundation\MaintenanceMode as MaintenanceModeContract;
use Illuminate\Contracts\View\Factory;
use Illuminate\Database\ConnectionInterface;
Expand All @@ -15,6 +16,7 @@
use Illuminate\Foundation\Exceptions\Renderer\Listener;
use Illuminate\Foundation\Exceptions\Renderer\Mappers\BladeMapper;
use Illuminate\Foundation\Exceptions\Renderer\Renderer;
use Illuminate\Foundation\Exceptions\Renderer\LaravelRenderer;
use Illuminate\Foundation\Http\HtmlDumper;
use Illuminate\Foundation\MaintenanceModeManager;
use Illuminate\Foundation\Precognition;
Expand Down Expand Up @@ -135,8 +137,6 @@ public function registerDumper()
* Register the "validate" macro on the request.
*
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
public function registerRequestValidation()
{
Expand Down Expand Up @@ -238,6 +238,10 @@ protected function registerExceptionRenderer()
});

$this->app->singleton(Listener::class);

$this->app->singleton(ExceptionRenderer::class, function ($app) {
return new LaravelRenderer($app[Renderer::class], $app['request']);
});
}

/**
Expand Down

0 comments on commit 4a030d9

Please sign in to comment.