-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fb48139
Showing
9 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# For more information about the properties used in | ||
# this file, please see the EditorConfig documentation: | ||
# http://editorconfig.org/ | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_size = 4 | ||
indent_style = space | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.{yml,js,json,css,scss,eslintrc}] | ||
indent_size = 2 | ||
indent_style = space | ||
|
||
[composer.json] | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Jonathon Menz | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# SilverStripe Custom Errors Module | ||
|
||
## Overview | ||
|
||
As an alternative to the Error Page module, this module provides themed error responses but makes developers responsible for the content of error messages instead of CMS users. | ||
|
||
## Installation | ||
|
||
``` | ||
$ composer require jonom/silverstripe-custom-errors | ||
``` | ||
|
||
## Configuration | ||
|
||
You can define default response content for each response code through the yml config API. Response codes need to be prefixed with the letter 'e' because numbers alone aren't valid SS config keys. Fields you specify will be passed through to a page template for rendering. You can specify a value only, or cast a value by specifying details in an arrray. Example: | ||
|
||
```yaml | ||
--- | ||
Name: my-custom-errors | ||
After: | ||
- '#custom-errors' | ||
--- | ||
JonoM\CustomErrors\CustomErrorControllerExtension: | ||
custom_fields: | ||
default: | ||
Content: | ||
Type: 'SilverStripe\ORM\FieldType\DBHTMLText' | ||
Value: '<p>Sorry, there was a problem with handling your request.</p>' | ||
e404: | ||
Title: 'Not Found' | ||
Content: | ||
Type: 'SilverStripe\ORM\FieldType\DBHTMLText' | ||
Value: '<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>' | ||
``` | ||
You can also specify a default controller and template for error responses. | ||
```yaml | ||
JonoM\CustomErrors\CustomErrorControllerExtension: | ||
default_controller: 'PageController' | ||
default_template: 'Page' # exclude .ss extension | ||
``` | ||
## Custom error responses | ||
You can call `$this->httpError($statusCode, $errorMessage)` from your controller and get a themed response, but if default content has been provided for the given status code, your `$errorMessage` won't be displayed. This is to ensure that you have some control over all of the error messages that a user may see, not just the ones that are triggered in your own code. | ||
|
||
To return a custom error response from a controller, instead of calling `$this->httpError()` you can use `$this->customError()` and pass through custom fields the same way you would if using `renderWith()`. Example: | ||
|
||
```php | ||
if ($somethingWentWrong) { | ||
$this->customError(404, [ | ||
'Title' => 'We couldn\'t find the droids you are looking for', | ||
'Content' => DBField::create_field('HTMLText', '<p>Please try another Cantina</p>') | ||
]); | ||
} | ||
``` | ||
|
||
Any fields you specify are merged with the defaults, so you only need to specify fields that you want to override. | ||
|
||
```php | ||
$this->customError(404, [ | ||
'Content' => DBField::create_field('HTMLText', '<p>Maybe try the Google</p>') | ||
]); | ||
``` | ||
|
||
You can also specify a controller and template to be used. | ||
|
||
```php | ||
$this->customError( | ||
404, | ||
[ | ||
'Content' => DBField::create_field('HTMLText', '<p>Here are some other droids you may be interested in:</p>'), | ||
'Droids' => Droid::get()->filterAny(['Language' => 'Beeps', 'Color' => 'Gold'])->limit(5) | ||
], | ||
'DroidHolder', | ||
'DroidHolderController' | ||
); | ||
``` | ||
|
||
## Maintainer contact | ||
|
||
[jonathonmenz.com](http://jonathonmenz.com) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
Name: custom-errors | ||
--- | ||
JonoM\CustomErrors\CustomErrorControllerExtension: | ||
default_controller: 'PageController' | ||
default_template: 'Page' # exclude .ss extension | ||
custom_fields: | ||
default: | ||
Content: | ||
Type: 'SilverStripe\ORM\FieldType\DBHTMLText' | ||
Value: '<p>Sorry, there was a problem with handling your request.</p>' | ||
e404: | ||
Title: 'Not Found' | ||
Content: | ||
Type: 'SilverStripe\ORM\FieldType\DBHTMLText' | ||
Value: '<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>' | ||
e500: | ||
Title: 'Internal Server Error' | ||
Content: | ||
Type: 'SilverStripe\ORM\FieldType\DBHTMLText' | ||
Value: '<p>Sorry, there was a problem with handling your request.</p>' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
Name: custom-errors-extensions | ||
--- | ||
SilverStripe\Control\Controller: | ||
extensions: | ||
- JonoM\CustomErrors\CustomErrorControllerExtension | ||
SilverStripe\Forms\Form: | ||
extensions: | ||
- JonoM\CustomErrors\CustomErrorControllerExtension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
When having discussions about this module in issues or pull request please adhere to the [SilverStripe Community Code of Conduct](https://docs.silverstripe.org/en/contributing/code_of_conduct). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "jonom/silverstripe-custom-errors", | ||
"description": "Themed custom error responses for SilverStripe CMS", | ||
"type": "silverstripe-vendormodule", | ||
"license": "MIT", | ||
"keywords": [ | ||
"silverstripe", | ||
"error", | ||
"errorpage" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "Jonathon Menz", | ||
"homepage": "http://jonathonmenz.com" | ||
} | ||
], | ||
"require": { | ||
"silverstripe/vendor-plugin": "^1.0", | ||
"silverstripe/cms": "^4" | ||
}, | ||
"replace": { | ||
"silverstripe/errorpage":"*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"JonoM\\CustomErrors\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"installer-name": "custom-errors" | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Contributing | ||
- Maintenance on this module is a shared effort of those who use it | ||
- To contribute improvements to the code, ensure you raise a pull request and discuss with the module maintainers | ||
- Please follow the SilverStripe [code contribution guidelines](https://docs.silverstripe.org/en/contributing/code/) and [Module Standard](https://docs.silverstripe.org/en/developer_guides/extending/modules/#module-standard) | ||
- Supply documentation that follows the [GitHub Flavored Markdown](https://help.github.com/articles/markdown-basics/) conventions | ||
- When having discussions about this module in issues or pull request please adhere to the [SilverStripe Community Code of Conduct](https://docs.silverstripe.org/en/contributing/code_of_conduct/) | ||
|
||
## Contributor license agreement | ||
By supplying code to this module in patches, tickets and pull requests, you agree to assign copyright | ||
of that code to Jonathon Menz, on the condition that these code changes are released under the | ||
same MIT license as the original module. We ask for this so that the ownership in the license is clear | ||
and unambiguous. By releasing this code under a permissive license such as MIT, this copyright assignment | ||
won't prevent you from using the code in any way you see fit. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace JonoM\CustomErrors; | ||
|
||
use Page; | ||
use PageController; | ||
use SilverStripe\Control\Director; | ||
use SilverStripe\Control\HTTPResponse; | ||
use SilverStripe\Control\HTTPResponse_Exception; | ||
use SilverStripe\Core\Config\Configurable; | ||
use SilverStripe\Core\Extension; | ||
use SilverStripe\Forms\TextField; | ||
use SilverStripe\ORM\FieldType\DBField; | ||
use SilverStripe\ORM\FieldType\DBHTMLText; | ||
|
||
/** | ||
* Enhances error handling for a controller with themed output | ||
* | ||
* 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. | ||
*/ | ||
class CustomErrorControllerExtension extends Extension | ||
{ | ||
use Configurable; | ||
|
||
private static $custom_fields = []; | ||
|
||
private static $default_controller = ''; | ||
|
||
private static $default_template = ''; | ||
|
||
/** | ||
* Used by {@see RequestHandler::httpError} | ||
* | ||
* @param int $statusCode | ||
* @param HTTPRequest $request | ||
* @param string $errorMessage | ||
* @throws HTTPResponse_Exception | ||
*/ | ||
public function onBeforeHTTPError($errorCode, $request, $errorMessage = null) | ||
{ | ||
// 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. | ||
$customFields = []; | ||
if (!isset($this->config()->get('custom_fields')['e' .$errorCode])) $customFields['Content'] = $errorMessage; | ||
return $this->customError($errorCode, $customFields); | ||
} | ||
|
||
public function defaultCustomFieldsFor($errorCode) | ||
{ | ||
// Get content from yml config for a default | ||
$defaultCustomFields = []; | ||
$configCustomFields = $this->config()->get('custom_fields'); | ||
$configCustomFields = isset($configCustomFields['e' . $errorCode]) ? $configCustomFields['e' . $errorCode] : $configCustomFields['default']; | ||
foreach ($configCustomFields as $fieldName => $fieldInfo) { | ||
// Create a db field if type is specified, otherwise pass plain text | ||
if (is_array($fieldInfo)) { | ||
$defaultCustomFields[$fieldName] = DBField::create_field($fieldInfo['Type'], $fieldInfo['Value']); | ||
} else { | ||
$defaultCustomFields[$fieldName] = $fieldInfo; | ||
} | ||
} | ||
return $defaultCustomFields; | ||
} | ||
|
||
|
||
public function customError($errorCode, $customFields, $template = null, $controllerType = null) | ||
{ | ||
// Simple/default response if ajax | ||
if (Director::is_ajax()) { | ||
throw new HTTPResponse_Exception($errorMessage, $errorCode); | ||
} | ||
// Otherwise build a themed response | ||
if (!$controllerType) $controllerType = $this->config()->get('default_controller'); | ||
if (!$template) $template = $this->config()->get('default_template'); | ||
$defaultCustomFields = $this->defaultCustomFieldsFor($errorCode); | ||
$response = new HTTPResponse(); | ||
$response->setStatusCode($errorCode); | ||
// Set some default properties, then override with custom ones if provided | ||
$customFields = array_merge( | ||
// Use title from config if set, otherwise fall back to framework definition | ||
['Title' => $response->getStatusDescription()], | ||
$defaultCustomFields, | ||
$customFields | ||
); | ||
$body = $controllerType::create()->renderWith($template, $customFields); | ||
$response->setBody($body); | ||
if ($response) { | ||
throw new HTTPResponse_Exception($response, $errorCode); | ||
} | ||
} | ||
} |