Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Allows HTTP exceptions to be thrown for views #47714

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');