mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #5556 from open-sausages/pulls/3/errors-http-response
API Enable Debug.friendly_error_httpcode to correctly set HTTP status code for errors
This commit is contained in:
commit
0dd1cc09d0
@ -40,6 +40,17 @@ class Debug {
|
|||||||
*/
|
*/
|
||||||
private static $friendly_error_header = 'There has been an error';
|
private static $friendly_error_header = 'There has been an error';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set to true to enable friendly errors to set a http response code corresponding to the error.
|
||||||
|
* If left false then error pages will be served as HTTP 200.
|
||||||
|
*
|
||||||
|
* Will be removed in 4.0, and fixed to on.
|
||||||
|
*
|
||||||
|
* @config
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private static $friendly_error_httpcode = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @config
|
* @config
|
||||||
* @var string The body of the message shown to users on the live site when a fatal error occurs.
|
* @var string The body of the message shown to users on the live site when a fatal error occurs.
|
||||||
@ -318,22 +329,21 @@ class Debug {
|
|||||||
* @return string HTML error message for non-ajax requests, plaintext for ajax-request.
|
* @return string HTML error message for non-ajax requests, plaintext for ajax-request.
|
||||||
*/
|
*/
|
||||||
public static function friendlyError($statusCode=500, $friendlyErrorMessage=null, $friendlyErrorDetail=null) {
|
public static function friendlyError($statusCode=500, $friendlyErrorMessage=null, $friendlyErrorDetail=null) {
|
||||||
|
// Ensure the error message complies with the HTTP 1.1 spec
|
||||||
if(!$friendlyErrorMessage) {
|
if(!$friendlyErrorMessage) {
|
||||||
$friendlyErrorMessage = Config::inst()->get('Debug', 'friendly_error_header');
|
$friendlyErrorMessage = Config::inst()->get('Debug', 'friendly_error_header');
|
||||||
}
|
}
|
||||||
|
$friendlyErrorMessage = strip_tags(str_replace(array("\n", "\r"), '', $friendlyErrorMessage));
|
||||||
|
|
||||||
if(!$friendlyErrorDetail) {
|
if(!$friendlyErrorDetail) {
|
||||||
$friendlyErrorDetail = Config::inst()->get('Debug', 'friendly_error_detail');
|
$friendlyErrorDetail = Config::inst()->get('Debug', 'friendly_error_detail');
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!headers_sent()) {
|
if(!headers_sent()) {
|
||||||
// Ensure the error message complies with the HTTP 1.1 spec
|
// Allow toggle between legacy behaviour and correctly setting HTTP response code
|
||||||
$msg = strip_tags(str_replace(array("\n", "\r"), '', $friendlyErrorMessage));
|
// In 4.0 this should be fixed to always set this response code.
|
||||||
if(Controller::has_curr()) {
|
if(Config::inst()->get('Debug', 'friendly_error_httpcode') || !Controller::has_curr()) {
|
||||||
$response = Controller::curr()->getResponse();
|
header($_SERVER['SERVER_PROTOCOL'] . " $statusCode $friendlyErrorMessage");
|
||||||
$response->setStatusCode($statusCode, $msg);
|
|
||||||
} else {
|
|
||||||
header($_SERVER['SERVER_PROTOCOL'] . " $statusCode $msg");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,3 +16,18 @@ was affected by these:
|
|||||||
outputted.
|
outputted.
|
||||||
* DataObject::isChanged() now defaults to only checking database fields. If you rely on this method
|
* DataObject::isChanged() now defaults to only checking database fields. If you rely on this method
|
||||||
for checking changes to non-db field properties, use getChangedFields() instead.
|
for checking changes to non-db field properties, use getChangedFields() instead.
|
||||||
|
|
||||||
|
### Error handling
|
||||||
|
|
||||||
|
Up until 3.4.0 error responses handled by SilverStripe have normally returned HTTP 200. The correct http response
|
||||||
|
code can be turned on by setting `Debug.friendly_error_httpcode` config to true. This option will be removed in
|
||||||
|
4.0 and fixed to always on.
|
||||||
|
|
||||||
|
|
||||||
|
:::yaml
|
||||||
|
---
|
||||||
|
Name: mydebug
|
||||||
|
---
|
||||||
|
Debug:
|
||||||
|
friendly_error_httpcode: true
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user