Skip to content

Commit

Permalink
Pipe through render and report exception methods (#36032)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Jan 25, 2021
1 parent 3464d02 commit 7e73bd2
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Illuminate\Session\TokenMismatchException;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Reflector;
use Illuminate\Support\ViewErrorBag;
use Illuminate\Validation\ValidationException;
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/View/Engines/CompilerEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Illuminate\View\Engines;

use ErrorException;
use Exception;
use Illuminate\View\Compilers\CompilerInterface;
use Illuminate\View\ViewException;

class CompilerEngine extends PhpEngine
{
Expand Down Expand Up @@ -74,7 +74,7 @@ public function get($path, array $data = [])
*/
protected function handleViewException(Exception $e, $obLevel)
{
$e = new ErrorException($this->getMessage($e), 0, 1, $e->getFile(), $e->getLine(), $e);
$e = new ViewException($this->getMessage($e), 0, 1, $e->getFile(), $e->getLine(), $e);

parent::handleViewException($e, $obLevel);
}
Expand Down
37 changes: 37 additions & 0 deletions src/Illuminate/View/ViewException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Illuminate\View;

use ErrorException;

class ViewException extends ErrorException
{
/**
* Report the exception.
*
* @return void
*/
public function report()
{
$exception = $this->getPrevious();

if ($exception && method_exists('report', $exception)) {
$exception->report();
}
}

/**
* Render the exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function render($request)
{
$exception = $this->getPrevious();

if ($exception && method_exists($exception, 'render')) {
return $exception->render($request);
}
}
}
36 changes: 36 additions & 0 deletions tests/Integration/View/RenderableViewExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Illuminate\Tests\Integration\View;

use Exception;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\View;
use Orchestra\Testbench\TestCase;

class RenderableViewExceptionTest extends TestCase
{
public function testRenderMethodOfExceptionThrownInViewGetsHandled()
{
Route::get('/', function () {
return View::make('renderable-exception');
});

$response = $this->get('/');

$response->assertSee('This is a renderable exception.');
}

protected function getEnvironmentSetUp($app)
{
$app['config']->set('view.paths', [__DIR__.'/templates']);
}
}

class RenderableException extends Exception
{
public function render($request)
{
return new Response('This is a renderable exception.');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@php
throw new Illuminate\Tests\Integration\View\RenderableException;
@endphp
1 change: 0 additions & 1 deletion tests/View/fixtures/nested/basic.php
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
Hello World

0 comments on commit 7e73bd2

Please sign in to comment.