diff --git a/_config/forms.yml b/_config/forms.yml index a6d1b214b..b3dc4d4ea 100644 --- a/_config/forms.yml +++ b/_config/forms.yml @@ -16,3 +16,7 @@ SilverStripe\Forms\Form: SilverStripe\Forms\FormField: extensions: - SilverStripe\Forms\FormMessageBootstrapExtension + +SilverStripe\Admin\LeftAndMain: + extensions: + - SilverStripe\Admin\LeftAndMainErrorExtension diff --git a/code/LeftAndMain.php b/code/LeftAndMain.php index af700f103..8e1f87c63 100644 --- a/code/LeftAndMain.php +++ b/code/LeftAndMain.php @@ -175,6 +175,11 @@ class LeftAndMain extends Controller implements PermissionProvider */ protected $pageID = null; + /** + * Set by {@link LeftAndMainErrorExtension} if an http error occurs + */ + private string $httpErrorMessage; + /** * Assign themes to use for cms * @@ -773,6 +778,32 @@ protected function init() Versioned::set_default_reading_mode(Versioned::get_reading_mode()); } + public function afterHandleRequest() + { + if ($this->response->isError()) { + $this->init(); + $errorCode = $this->response->getStatusCode(); + $errorType = $this->response->getStatusDescription(); + $defaultMessage = _t( + self::class . '.ErrorMessage', + 'Sorry, it seems there was a {errorcode} error.', + ['errorcode' => $errorCode] + ); + $this->response = HTTPResponse::create($this->render([ + 'Title' => $this->getApplicationName() . ' - ' . $errorType, + 'Content' => $this->renderWith($this->getTemplatesWithSuffix('_NotFound'), [ + 'ErrorCode' => $errorCode, + 'ErrorType' => $errorType, + 'Message' => DBField::create_field( + 'HTMLFragment', + _t(self::class . '.ErrorMessage' . $errorCode, $defaultMessage) + ), + ]), + ]), $errorCode, $errorType); + } + parent::afterHandleRequest(); + } + public function handleRequest(HTTPRequest $request) { try { @@ -1998,4 +2029,23 @@ public function getVersionProvider() { return $this->versionProvider; } + + /** + * Get the HTTP error message if one has occurred during HandleRequest. + */ + public function getHttpErrorMessage(): string + { + return $this->httpErrorMessage; + } + + /** + * Set the HTTP error message when one occurs during HandleRequest. + * Called by {@link LeftAndMainErrorExtension} + * @internal + */ + public function setHttpErrorMessage(string $message): self + { + $this->httpErrorMessage = $message; + return $this; + } } diff --git a/code/LeftAndMainErrorExtension.php b/code/LeftAndMainErrorExtension.php new file mode 100644 index 000000000..6f8fe0f78 --- /dev/null +++ b/code/LeftAndMainErrorExtension.php @@ -0,0 +1,21 @@ +owner->setHttpErrorMessage($errorMessage); + } +} diff --git a/lang/en.yml b/lang/en.yml index f133800c4..eac3cb08a 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -33,6 +33,12 @@ en: CollapsePanel: 'Collapse panel' DELETED: Deleted. DropdownBatchActionsDefault: 'Choose an action...' + ErrorDetail: 'Error detail: {detail}' + ErrorMessage: '
Sorry, it seems there was a {errorcode} error.
' + ErrorMessage403: 'Sorry, it seems the action you were trying to perform is forbidden.
' + ErrorMessage404: 'Sorry, it seems you were trying to access a page that doesn''t exist.
' + ErrorMessage500: 'Sorry, it seems there was an internal server error.
' + ErrorMessage503: 'Sorry, it seems the service is temporarily unavailable.
' ExpandPanel: 'Expand panel' HelpMenu: 'Help menu' LOGOUT: 'Log out' diff --git a/templates/SilverStripe/Admin/Includes/LeftAndMain_NotFound.ss b/templates/SilverStripe/Admin/Includes/LeftAndMain_NotFound.ss new file mode 100644 index 000000000..9f03eed30 --- /dev/null +++ b/templates/SilverStripe/Admin/Includes/LeftAndMain_NotFound.ss @@ -0,0 +1,24 @@ ++ <%t SilverStripe\Admin\LeftAndMain.ErrorDetail "Error detail: {detail}" detail=$HttpErrorMessage %> +
+ <% end_if %> +