Skip to content

Commit

Permalink
[9.x] Add support for 4xx/5xx fallback error views (#38877)
Browse files Browse the repository at this point in the history
* add fallback 4xx/5xx error views

* cs
  • Loading branch information
SjorsO authored Sep 20, 2021
1 parent 2b2420b commit 818d736
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ protected function renderHttpException(HttpExceptionInterface $e)
{
$this->registerErrorViewPaths();

if (view()->exists($view = $this->getHttpExceptionView($e))) {
if ($view = $this->getHttpExceptionView($e)) {
return response()->view($view, [
'errors' => new ViewErrorBag,
'exception' => $e,
Expand All @@ -568,11 +568,23 @@ protected function registerErrorViewPaths()
* Get the view used to render HTTP exceptions.
*
* @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e
* @return string
* @return string|null
*/
protected function getHttpExceptionView(HttpExceptionInterface $e)
{
return "errors::{$e->getStatusCode()}";
$view = 'errors::'.$e->getStatusCode();

if (view()->exists($view)) {
return $view;
}

$view = substr($view, 0, -2).'xx';

if (view()->exists($view)) {
return $view;
}

return null;
}

/**
Expand Down
63 changes: 63 additions & 0 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Database\RecordsNotFoundException;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Http\RedirectResponse;
Expand Down Expand Up @@ -285,6 +286,68 @@ public function testRecordsNotFoundReturns404WithoutReporting()

$this->handler->report(new RecordsNotFoundException);
}

public function testItReturnsSpecificErrorViewIfExists()
{
$viewFactory = m::mock(stdClass::class);
$viewFactory->shouldReceive('exists')->with('errors::502')->andReturn(true);

$this->container->singleton(ViewFactory::class, function () use ($viewFactory) {
return $viewFactory;
});

$handler = new class($this->container) extends Handler
{
public function getErrorView($e)
{
return $this->getHttpExceptionView($e);
}
};

$this->assertSame('errors::502', $handler->getErrorView(new HttpException(502)));
}

public function testItReturnsFallbackErrorViewIfExists()
{
$viewFactory = m::mock(stdClass::class);
$viewFactory->shouldReceive('exists')->once()->with('errors::502')->andReturn(false);
$viewFactory->shouldReceive('exists')->once()->with('errors::5xx')->andReturn(true);

$this->container->singleton(ViewFactory::class, function () use ($viewFactory) {
return $viewFactory;
});

$handler = new class($this->container) extends Handler
{
public function getErrorView($e)
{
return $this->getHttpExceptionView($e);
}
};

$this->assertSame('errors::5xx', $handler->getErrorView(new HttpException(502)));
}

public function testItReturnsNullIfNoErrorViewExists()
{
$viewFactory = m::mock(stdClass::class);
$viewFactory->shouldReceive('exists')->once()->with('errors::404')->andReturn(false);
$viewFactory->shouldReceive('exists')->once()->with('errors::4xx')->andReturn(false);

$this->container->singleton(ViewFactory::class, function () use ($viewFactory) {
return $viewFactory;
});

$handler = new class($this->container) extends Handler
{
public function getErrorView($e)
{
return $this->getHttpExceptionView($e);
}
};

$this->assertNull($handler->getErrorView(new HttpException(404)));
}
}

class CustomException extends Exception
Expand Down

0 comments on commit 818d736

Please sign in to comment.