Skip to content

Commit

Permalink
[6.x] Backport of Pipe through render and report exception methods (#…
Browse files Browse the repository at this point in the history
…36037)

* Pipe through render and report exception methods (#36032)

* Fix method_exists call

* Re-add facade
  • Loading branch information
driesvints committed Jan 25, 2021
1 parent 6e79268 commit fdf46c3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
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($exception, 'report')) {
$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 fdf46c3

Please sign in to comment.