From 7783956b18333f41ee005fbb325b03baf708bb42 Mon Sep 17 00:00:00 2001 From: Andrew Paxley Date: Mon, 25 Sep 2023 16:46:40 +1300 Subject: [PATCH] ENH add config for allowed_error_codes --- README.md | 13 +++++++++++++ src/ErrorPage.php | 41 +++++++++++++++++++++++++++-------------- tests/ErrorPageTest.php | 17 +++++++++++++++++ 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index c6187ef..55d7a5d 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,19 @@ SilverStripe\ErrorPage\ErrorPage: dev_append_error_message: false ``` +### Limiting options 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. diff --git a/src/ErrorPage.php b/src/ErrorPage.php index 82314e1..448efd9 100644 --- a/src/ErrorPage.php +++ b/src/ErrorPage.php @@ -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")'; @@ -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 * @@ -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(); @@ -226,8 +232,8 @@ protected function requireDefaultRecordFixture($defaultData) */ protected function getDefaultRecords() { - $data = array( - array( + $data = [ + [ 'ErrorCode' => 404, 'Title' => _t('SilverStripe\\ErrorPage\\ErrorPage.DEFAULTERRORPAGETITLE', 'Page not found'), 'Content' => _t( @@ -235,16 +241,16 @@ protected function getDefaultRecords() '

Sorry, it seems you were trying to access a page that doesn\'t exist.

' . '

Please check the spelling of the URL you were trying to access and try again.

' ) - ), - array( + ], + [ 'ErrorCode' => 500, 'Title' => _t('SilverStripe\\ErrorPage\\ErrorPage.DEFAULTSERVERERRORPAGETITLE', 'Server error'), 'Content' => _t( 'SilverStripe\\ErrorPage\\ErrorPage.DEFAULTSERVERERRORPAGECONTENT', '

Sorry, there was a problem with handling your request.

' ) - ) - ); + ] + ]; $this->extend('getDefaultRecords', $data); @@ -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'), @@ -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)); } /** diff --git a/tests/ErrorPageTest.php b/tests/ErrorPageTest.php index 8dfff69..1bc0b39 100644 --- a/tests/ErrorPageTest.php +++ b/tests/ErrorPageTest.php @@ -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); + } }