Skip to content

Commit

Permalink
Allows HTTP exceptions to be thrown for views (#47714)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro authored Jul 11, 2023
1 parent 5985650 commit 8098f6d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Illuminate/View/Engines/CompilerEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\CompilerInterface;
use Illuminate\View\ViewException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Throwable;

class CompilerEngine extends PhpEngine
Expand Down Expand Up @@ -100,6 +101,10 @@ public function get($path, array $data = [])
*/
protected function handleViewException(Throwable $e, $obLevel)
{
if ($e instanceof HttpException) {
parent::handleViewException($e, $obLevel);
}

$e = new ViewException($this->getMessage($e), 0, 1, $e->getFile(), $e->getLine(), $e);

parent::handleViewException($e, $obLevel);
Expand Down
25 changes: 25 additions & 0 deletions tests/View/ViewCompilerEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\View\ViewException;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Exception\HttpException;

class ViewCompilerEngineTest extends TestCase
{
Expand Down Expand Up @@ -43,6 +44,30 @@ public function testViewsAreNotRecompiledIfTheyAreNotExpired()
', $results);
}

public function testRegularExceptionsAreReThrownAsViewExceptions()
{
$engine = $this->getEngine();
$engine->getCompiler()->shouldReceive('getCompiledPath')->with(__DIR__.'/fixtures/foo.php')->andReturn(__DIR__.'/fixtures/regular-exception.php');
$engine->getCompiler()->shouldReceive('isExpired')->once()->andReturn(false);

$this->expectException(ViewException::class);
$this->expectExceptionMessage('regular exception message');

$engine->get(__DIR__.'/fixtures/foo.php');
}

public function testHttpExceptionsAreNotReThrownAsViewExceptions()
{
$engine = $this->getEngine();
$engine->getCompiler()->shouldReceive('getCompiledPath')->with(__DIR__.'/fixtures/foo.php')->andReturn(__DIR__.'/fixtures/http-exception.php');
$engine->getCompiler()->shouldReceive('isExpired')->once()->andReturn(false);

$this->expectException(HttpException::class);
$this->expectExceptionMessage('http exception message');

$engine->get(__DIR__.'/fixtures/foo.php');
}

public function testThatViewsAreNotAskTwiceIfTheyAreExpired()
{
$engine = $this->getEngine();
Expand Down
5 changes: 5 additions & 0 deletions tests/View/fixtures/http-exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Symfony\Component\HttpKernel\Exception\HttpException;

throw new HttpException(403, 'http exception message');
3 changes: 3 additions & 0 deletions tests/View/fixtures/regular-exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

throw new Exception('regular exception message');

0 comments on commit 8098f6d

Please sign in to comment.