Skip to content

Commit

Permalink
ENH add config for allowed_error_codes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewandante committed Sep 25, 2023
1 parent a3f84c0 commit 85f05da
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/ErrorPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@
*/
class ErrorPage extends Page
{
private static $db = array(
private static $db = [
"ErrorCode" => "Int",
);
];

private static $defaults = array(
private static $defaults = [
"ShowInMenus" => 0,
"ShowInSearch" => 0,
"ErrorCode" => 400
);
];

private static $table_name = 'ErrorPage';

private static $allowed_children = array();
private static $allowed_children = [];

private static $description = 'Custom content for different error cases (e.g. "Page not found")';

Expand Down Expand Up @@ -80,6 +80,15 @@ class ErrorPage extends Page
*/
private static $store_filepath = null;

/**
* An array of error codes to allow in the CMS
* If unset, defaults to all available codes (see self::getCodes())
*
* @config
* @var ?array
*/
private static $allowed_error_codes = null;

/**
* @param $member
*
Expand All @@ -106,9 +115,9 @@ public static function response_for($statusCode, $errorMessage = null)
// first attempt to dynamically generate the error page
/** @var ErrorPage $errorPage */
$errorPage = ErrorPage::get()
->filter(array(
->filter([
"ErrorCode" => $statusCode
))->first();
])->first();

if ($errorPage) {
Requirements::clear();
Expand Down Expand Up @@ -226,25 +235,25 @@ protected function requireDefaultRecordFixture($defaultData)
*/
protected function getDefaultRecords()
{
$data = array(
array(
$data = [
[
'ErrorCode' => 404,
'Title' => _t('SilverStripe\\ErrorPage\\ErrorPage.DEFAULTERRORPAGETITLE', 'Page not found'),
'Content' => _t(
'SilverStripe\\ErrorPage\\ErrorPage.DEFAULTERRORPAGECONTENT',
'<p>Sorry, it seems you were trying to access a page that doesn\'t exist.</p>'
. '<p>Please check the spelling of the URL you were trying to access and try again.</p>'
)
),
array(
],
[
'ErrorCode' => 500,
'Title' => _t('SilverStripe\\ErrorPage\\ErrorPage.DEFAULTSERVERERRORPAGETITLE', 'Server error'),
'Content' => _t(
'SilverStripe\\ErrorPage\\ErrorPage.DEFAULTSERVERERRORPAGECONTENT',
'<p>Sorry, there was a problem with handling your request.</p>'
)
)
);
]
];

$this->extend('getDefaultRecords', $data);

Expand Down Expand Up @@ -384,7 +393,7 @@ public static function get_content_for_errorcode($statusCode)

protected function getCodes()
{
return [
$allCodes = [
400 => _t('SilverStripe\\ErrorPage\\ErrorPage.CODE_400', '400 - Bad Request'),
401 => _t('SilverStripe\\ErrorPage\\ErrorPage.CODE_401', '401 - Unauthorized'),
402 => _t('SilverStripe\\ErrorPage\\ErrorPage.CODE_402', '402 - Payment Required'),
Expand Down Expand Up @@ -426,6 +435,13 @@ protected function getCodes()
510 => _t('SilverStripe\\ErrorPage\\ErrorPage.CODE_510', '510 - Not Extended'),
511 => _t('SilverStripe\\ErrorPage\\ErrorPage.CODE_511', '511 - Network Authentication Required'),
];

$allowedCodes = self::config()->get('allowed_error_codes');
if ($allowedCodes === null) {
return $allCodes;
}

return array_intersect_key($allCodes, array_flip($allowedCodes));
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/ErrorPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected function setUp(): void
// Set temporary asset backend store
TestAssetStore::activate('ErrorPageTest');
Config::modify()->set(ErrorPage::class, 'enable_static_file', true);
Config::modify()->set(ErrorPage::class, 'allowed_error_codes', null);
$this->logInWithPermission('ADMIN');
}

Expand Down Expand Up @@ -283,4 +284,21 @@ public function testRequiredRecords()
$this->expectOutputRegex('/.*500 error page refreshed.*/');
ErrorPage::singleton()->requireDefaultRecords();
}

public function testAllowedAllErrorCodes()
{
$page = $this->objFromFixture(ErrorPage::class, '404');
$allCodes = $page->getCodes();
$this->assertCount(40, $allCodes);
}

public function testAllowedErrorCodes()
{
Config::modify()->set(ErrorPage::class, 'allowed_error_codes', [400, 500]);
$page = $this->objFromFixture(ErrorPage::class, '404');
$codes = $page->getCodes();
$this->assertCount(2, $codes);
$this->assertArrayHasKey(400, $codes);
$this->assertArrayHasKey(500, $codes);
}
}

0 comments on commit 85f05da

Please sign in to comment.