Skip to content

Commit

Permalink
Render error page after ErrorPageException #1887
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbestle committed Sep 29, 2019
1 parent 8862c74 commit 591f132
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/Cms/App.php
Expand Up @@ -4,6 +4,7 @@

use Kirby\Data\Data;
use Kirby\Email\PHPMailer as Emailer;
use Kirby\Exception\ErrorPageException;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Http\Request;
Expand Down Expand Up @@ -586,7 +587,11 @@ public function io($input)

// Pages
if (is_a($input, 'Kirby\Cms\Page')) {
$html = $input->render();
try {
$html = $input->render();
} catch (ErrorPageException $e) {
return $this->io($e);
}

if ($input->isErrorPage() === true) {
if ($response->code() === null) {
Expand Down
81 changes: 77 additions & 4 deletions tests/Cms/App/AppIoTest.php
Expand Up @@ -2,7 +2,7 @@

namespace Kirby\Cms;

use Exception;
use Kirby\Exception\Exception;
use Kirby\Http\Response;
use PHPUnit\Framework\TestCase;

Expand All @@ -12,19 +12,42 @@ public function app()
{
return new App([
'roots' => [
'index' => '/dev/null'
'index' => '/dev/null',
'templates' => __DIR__ . '/fixtures/AppIoTest/templates'
]
]);
}

public function testException()
{
$response = $this->app()->io(new Exception('Nope'));
$response = $this->app()->io(new Exception([
'fallback' => 'Nope',
'httpCode' => 501
]));

$this->assertEquals(500, $response->code());
$this->assertEquals(501, $response->code());
$this->assertEquals('Nope', $response->body());
}

public function testExceptionErrorPage()
{
$app = $this->app()->clone([
'site' => [
'children' => [
[
'slug' => 'error',
'template' => 'error'
]
]
]
]);

$response = $app->io(new Exception('Nope'));

$this->assertEquals(500, $response->code());
$this->assertEquals('Error: Nope', $response->body());
}

public function testExceptionWithInvalidHttpCode()
{
$response = $this->app()->io(new Exception('Nope', 8000));
Expand Down Expand Up @@ -64,6 +87,56 @@ public function testResponse()
$this->assertEquals($input, $response);
}

public function testPage()
{
$input = new Page([
'slug' => 'test',
'template' => 'test'
]);

$response = $this->app()->io($input);

$this->assertEquals(200, $response->code());
$this->assertEquals('Test template', $response->body());
}

public function testPageErrorPageException()
{
$input = new Page([
'slug' => 'test',
'template' => 'errorpage-exception'
]);

$response = $this->app()->io($input);

$this->assertEquals(403, $response->code());
$this->assertEquals('Exception message', $response->body());
}

public function testPageErrorPageExceptionErrorPage()
{
$app = $this->app()->clone([
'site' => [
'children' => [
[
'slug' => 'error',
'template' => 'error'
]
]
]
]);

$input = new Page([
'slug' => 'test',
'template' => 'errorpage-exception'
]);

$response = $app->io($input);

$this->assertEquals(403, $response->code());
$this->assertEquals('Error: Exception message', $response->body());
}

public function testString()
{
$response = $this->app()->io('Test');
Expand Down
1 change: 1 addition & 0 deletions tests/Cms/App/fixtures/AppIoTest/templates/error.php
@@ -0,0 +1 @@
Error: <?= $errorMessage ?>
@@ -0,0 +1,6 @@
<?php

throw new Kirby\Exception\ErrorPageException([
'fallback' => 'Exception message',
'httpCode' => 403
]);
1 change: 1 addition & 0 deletions tests/Cms/App/fixtures/AppIoTest/templates/test.php
@@ -0,0 +1 @@
Test template

0 comments on commit 591f132

Please sign in to comment.