Skip to content

Commit 8098f6d

Browse files
authored
Allows HTTP exceptions to be thrown for views (#47714)
1 parent 5985650 commit 8098f6d

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

src/Illuminate/View/Engines/CompilerEngine.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Filesystem\Filesystem;
66
use Illuminate\View\Compilers\CompilerInterface;
77
use Illuminate\View\ViewException;
8+
use Symfony\Component\HttpKernel\Exception\HttpException;
89
use Throwable;
910

1011
class CompilerEngine extends PhpEngine
@@ -100,6 +101,10 @@ public function get($path, array $data = [])
100101
*/
101102
protected function handleViewException(Throwable $e, $obLevel)
102103
{
104+
if ($e instanceof HttpException) {
105+
parent::handleViewException($e, $obLevel);
106+
}
107+
103108
$e = new ViewException($this->getMessage($e), 0, 1, $e->getFile(), $e->getLine(), $e);
104109

105110
parent::handleViewException($e, $obLevel);

tests/View/ViewCompilerEngineTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\View\ViewException;
1212
use Mockery as m;
1313
use PHPUnit\Framework\TestCase;
14+
use Symfony\Component\HttpKernel\Exception\HttpException;
1415

1516
class ViewCompilerEngineTest extends TestCase
1617
{
@@ -43,6 +44,30 @@ public function testViewsAreNotRecompiledIfTheyAreNotExpired()
4344
', $results);
4445
}
4546

47+
public function testRegularExceptionsAreReThrownAsViewExceptions()
48+
{
49+
$engine = $this->getEngine();
50+
$engine->getCompiler()->shouldReceive('getCompiledPath')->with(__DIR__.'/fixtures/foo.php')->andReturn(__DIR__.'/fixtures/regular-exception.php');
51+
$engine->getCompiler()->shouldReceive('isExpired')->once()->andReturn(false);
52+
53+
$this->expectException(ViewException::class);
54+
$this->expectExceptionMessage('regular exception message');
55+
56+
$engine->get(__DIR__.'/fixtures/foo.php');
57+
}
58+
59+
public function testHttpExceptionsAreNotReThrownAsViewExceptions()
60+
{
61+
$engine = $this->getEngine();
62+
$engine->getCompiler()->shouldReceive('getCompiledPath')->with(__DIR__.'/fixtures/foo.php')->andReturn(__DIR__.'/fixtures/http-exception.php');
63+
$engine->getCompiler()->shouldReceive('isExpired')->once()->andReturn(false);
64+
65+
$this->expectException(HttpException::class);
66+
$this->expectExceptionMessage('http exception message');
67+
68+
$engine->get(__DIR__.'/fixtures/foo.php');
69+
}
70+
4671
public function testThatViewsAreNotAskTwiceIfTheyAreExpired()
4772
{
4873
$engine = $this->getEngine();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use Symfony\Component\HttpKernel\Exception\HttpException;
4+
5+
throw new HttpException(403, 'http exception message');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
throw new Exception('regular exception message');

0 commit comments

Comments
 (0)