Skip to content

Commit fdf46c3

Browse files
authored
[6.x] Backport of Pipe through render and report exception methods (#36037)
* Pipe through render and report exception methods (#36032) * Fix method_exists call * Re-add facade
1 parent 6e79268 commit fdf46c3

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

src/Illuminate/View/Engines/CompilerEngine.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Illuminate\View\Engines;
44

5-
use ErrorException;
65
use Exception;
76
use Illuminate\View\Compilers\CompilerInterface;
7+
use Illuminate\View\ViewException;
88

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

7979
parent::handleViewException($e, $obLevel);
8080
}

src/Illuminate/View/ViewException.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Illuminate\View;
4+
5+
use ErrorException;
6+
7+
class ViewException extends ErrorException
8+
{
9+
/**
10+
* Report the exception.
11+
*
12+
* @return void
13+
*/
14+
public function report()
15+
{
16+
$exception = $this->getPrevious();
17+
18+
if ($exception && method_exists($exception, 'report')) {
19+
$exception->report();
20+
}
21+
}
22+
23+
/**
24+
* Render the exception into an HTTP response.
25+
*
26+
* @param \Illuminate\Http\Request $request
27+
* @return \Illuminate\Http\Response
28+
*/
29+
public function render($request)
30+
{
31+
$exception = $this->getPrevious();
32+
33+
if ($exception && method_exists($exception, 'render')) {
34+
return $exception->render($request);
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\View;
4+
5+
use Exception;
6+
use Illuminate\Http\Response;
7+
use Illuminate\Support\Facades\Route;
8+
use Illuminate\Support\Facades\View;
9+
use Orchestra\Testbench\TestCase;
10+
11+
class RenderableViewExceptionTest extends TestCase
12+
{
13+
public function testRenderMethodOfExceptionThrownInViewGetsHandled()
14+
{
15+
Route::get('/', function () {
16+
return View::make('renderable-exception');
17+
});
18+
19+
$response = $this->get('/');
20+
21+
$response->assertSee('This is a renderable exception.');
22+
}
23+
24+
protected function getEnvironmentSetUp($app)
25+
{
26+
$app['config']->set('view.paths', [__DIR__.'/templates']);
27+
}
28+
}
29+
30+
class RenderableException extends Exception
31+
{
32+
public function render($request)
33+
{
34+
return new Response('This is a renderable exception.');
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@php
2+
throw new Illuminate\Tests\Integration\View\RenderableException;
3+
@endphp

tests/View/fixtures/nested/basic.php

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
Hello World

0 commit comments

Comments
 (0)