Skip to content

[9.x] Add support for 4xx/5xx fallback error views #38877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 20, 2021
Merged
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
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