|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JonoM\CustomErrors; |
| 4 | + |
| 5 | +use Page; |
| 6 | +use PageController; |
| 7 | +use SilverStripe\Control\Director; |
| 8 | +use SilverStripe\Control\HTTPResponse; |
| 9 | +use SilverStripe\Control\HTTPResponse_Exception; |
| 10 | +use SilverStripe\Core\Config\Configurable; |
| 11 | +use SilverStripe\Core\Extension; |
| 12 | +use SilverStripe\Forms\TextField; |
| 13 | +use SilverStripe\ORM\FieldType\DBField; |
| 14 | +use SilverStripe\ORM\FieldType\DBHTMLText; |
| 15 | + |
| 16 | +/** |
| 17 | + * Enhances error handling for a controller with themed output |
| 18 | + * |
| 19 | + * Precedence: error messages passed in $controller->httpError() will only be used if no yml config content is available. Use $controller->customError() to render an error response with custom content. |
| 20 | + */ |
| 21 | +class CustomErrorControllerExtension extends Extension |
| 22 | +{ |
| 23 | + use Configurable; |
| 24 | + |
| 25 | + private static $custom_fields = []; |
| 26 | + |
| 27 | + private static $default_controller = ''; |
| 28 | + |
| 29 | + private static $default_template = ''; |
| 30 | + |
| 31 | + /** |
| 32 | + * Used by {@see RequestHandler::httpError} |
| 33 | + * |
| 34 | + * @param int $statusCode |
| 35 | + * @param HTTPRequest $request |
| 36 | + * @param string $errorMessage |
| 37 | + * @throws HTTPResponse_Exception |
| 38 | + */ |
| 39 | + public function onBeforeHTTPError($errorCode, $request, $errorMessage = null) |
| 40 | + { |
| 41 | + // Use provided error message only if no content is specified in yml config. This is because the error messages used in non-user code when calling ->httpError may not be suitable to show to a user. |
| 42 | + $customFields = []; |
| 43 | + if (!isset($this->config()->get('custom_fields')['e' .$errorCode])) $customFields['Content'] = $errorMessage; |
| 44 | + return $this->customError($errorCode, $customFields); |
| 45 | + } |
| 46 | + |
| 47 | + public function defaultCustomFieldsFor($errorCode) |
| 48 | + { |
| 49 | + // Get content from yml config for a default |
| 50 | + $defaultCustomFields = []; |
| 51 | + $configCustomFields = $this->config()->get('custom_fields'); |
| 52 | + $configCustomFields = isset($configCustomFields['e' . $errorCode]) ? $configCustomFields['e' . $errorCode] : $configCustomFields['default']; |
| 53 | + foreach ($configCustomFields as $fieldName => $fieldInfo) { |
| 54 | + // Create a db field if type is specified, otherwise pass plain text |
| 55 | + if (is_array($fieldInfo)) { |
| 56 | + $defaultCustomFields[$fieldName] = DBField::create_field($fieldInfo['Type'], $fieldInfo['Value']); |
| 57 | + } else { |
| 58 | + $defaultCustomFields[$fieldName] = $fieldInfo; |
| 59 | + } |
| 60 | + } |
| 61 | + return $defaultCustomFields; |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + public function customError($errorCode, $customFields, $template = null, $controllerType = null) |
| 66 | + { |
| 67 | + // Simple/default response if ajax |
| 68 | + if (Director::is_ajax()) { |
| 69 | + throw new HTTPResponse_Exception($errorMessage, $errorCode); |
| 70 | + } |
| 71 | + // Otherwise build a themed response |
| 72 | + if (!$controllerType) $controllerType = $this->config()->get('default_controller'); |
| 73 | + if (!$template) $template = $this->config()->get('default_template'); |
| 74 | + $defaultCustomFields = $this->defaultCustomFieldsFor($errorCode); |
| 75 | + $response = new HTTPResponse(); |
| 76 | + $response->setStatusCode($errorCode); |
| 77 | + // Set some default properties, then override with custom ones if provided |
| 78 | + $customFields = array_merge( |
| 79 | + // Use title from config if set, otherwise fall back to framework definition |
| 80 | + ['Title' => $response->getStatusDescription()], |
| 81 | + $defaultCustomFields, |
| 82 | + $customFields |
| 83 | + ); |
| 84 | + $body = $controllerType::create()->renderWith($template, $customFields); |
| 85 | + $response->setBody($body); |
| 86 | + if ($response) { |
| 87 | + throw new HTTPResponse_Exception($response, $errorCode); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments