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 5a88a52
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ SilverStripe\ErrorPage\ErrorPage:
dev_append_error_message: false
```
### Limiting optios in the CMS
By default, all available error codes are present in the dropdown in the CMS. This can be overwhelming and there are a few (looking at you, 418) that can
be confusing. To that end, you can limit the codes in the dropdown with the config value `allowed_error_codes` like so:

```yml
SilverStripe\ErrorPage\ErrorPage:
allowed_error_codes:
- 400
- 403
- 404
- 500
```

## Reporting Issues

Please [create an issue](http://github.com/silverstripe/silverstripe-errorpage/issues) for any bugs you've found, or features you're missing.
41 changes: 27 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,12 @@ class ErrorPage extends Page
*/
private static $store_filepath = null;

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

/**
* @param $member
*
Expand All @@ -106,9 +112,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 +232,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 +390,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 +432,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 = static::config()->get('allowed_error_codes');
if ($allowedCodes === null) {
return $allCodes;
}

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

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/ErrorPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,21 @@ public function testRequiredRecords()
$this->expectOutputRegex('/.*500 error page refreshed.*/');
ErrorPage::singleton()->requireDefaultRecords();
}

public function testAllowedAllErrorCodes()
{
$page = new ErrorPage();
$allCodes = $page->getCMSFields()->fieldByName('ErrorCode')->getSource();
$this->assertCount(40, $allCodes);
}

public function testAllowedErrorCodes()
{
Config::modify()->set(ErrorPage::class, 'allowed_error_codes', [400, 500]);
$page = new ErrorPage();
$codes = $page->getCMSFields()->fieldByName('ErrorCode')->getSource();
$this->assertCount(2, $codes);
$this->assertArrayHasKey(400, $codes);
$this->assertArrayHasKey(500, $codes);
}
}

0 comments on commit 5a88a52

Please sign in to comment.