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 5493817
Show file tree
Hide file tree
Showing 5 changed files with 83 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.
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
19 changes: 19 additions & 0 deletions tests/ErrorPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\ErrorPage\ErrorPage;
use SilverStripe\ErrorPage\Tests\Fake\FakeErrorPage;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Versioned\Versioned;
Expand All @@ -40,6 +41,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 +285,21 @@ public function testRequiredRecords()
$this->expectOutputRegex('/.*500 error page refreshed.*/');
ErrorPage::singleton()->requireDefaultRecords();
}

public function testAllowedAllErrorCodes()
{
$page = $this->objFromFixture(FakeErrorPage::class, '418');
$allCodes = $page->codesForDropdown();
$this->assertCount(40, $allCodes);
}

public function testAllowedErrorCodes()
{
Config::modify()->set(ErrorPage::class, 'allowed_error_codes', [400, 500]);
$page = $this->objFromFixture(FakeErrorPage::class, '418');
$codes = $page->codesForDropdown();
$this->assertCount(2, $codes);
$this->assertArrayHasKey(400, $codes);
$this->assertArrayHasKey(500, $codes);
}
}
7 changes: 7 additions & 0 deletions tests/ErrorPageTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ SilverStripe\ErrorPage\ErrorPage:
Content: You do not have permission to view this page
ErrorCode: 403

SilverStripe\ErrorPage\Tests\Fake\FakeErrorPage:
418:
Title: "I'm a teapot"
URLSegment: im-a-teapot
Content: "I'm a teapot"
ErrorCode: 418

SilverStripe\CMS\Model\SiteTree:
home:
Title: Home
Expand Down
14 changes: 14 additions & 0 deletions tests/Fake/FakeErrorPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace SilverStripe\ErrorPage\Tests\Fake;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ErrorPage\ErrorPage;

class FakeErrorPage extends ErrorPage implements TestOnly
{
public function codesForDropdown()
{
return $this->getCodes();
}
}

0 comments on commit 5493817

Please sign in to comment.