mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #2262 from hafriedlander/fix/flush_24
FIX Nice errors and allows flush on module removal
This commit is contained in:
commit
a787dddeab
@ -3,16 +3,13 @@
|
|||||||
/**
|
/**
|
||||||
* Class ErrorControlChain
|
* Class ErrorControlChain
|
||||||
*
|
*
|
||||||
* Runs a set of steps, optionally suppressing (but recording) any errors (even fatal ones) that occur in each step.
|
* Runs a set of steps, optionally suppressing uncaught errors or exceptions which would otherwise be fatal that
|
||||||
* If an error does occur, subsequent steps are normally skipped, but can optionally be run anyway
|
* occur in each step. If an error does occur, subsequent steps are normally skipped, but can optionally be run anyway.
|
||||||
*
|
|
||||||
* Normal errors are suppressed even past the end of the chain. Fatal errors are only suppressed until the end
|
|
||||||
* of the chain - the request will then die silently.
|
|
||||||
*
|
*
|
||||||
* Usage:
|
* Usage:
|
||||||
*
|
*
|
||||||
* $chain = new ErrorControlChain();
|
* $chain = new ErrorControlChain();
|
||||||
* $chain->then($callback1)->then($callback2)->then(true, $callback3)->execute();
|
* $chain->then($callback1)->then($callback2)->thenIfErrored($callback3)->execute();
|
||||||
*
|
*
|
||||||
* WARNING: This class is experimental and designed specifically for use pre-startup in main.php
|
* WARNING: This class is experimental and designed specifically for use pre-startup in main.php
|
||||||
* It will likely be heavily refactored before the release of 3.2
|
* It will likely be heavily refactored before the release of 3.2
|
||||||
@ -28,6 +25,9 @@ class ErrorControlChain {
|
|||||||
/** We can't unregister_shutdown_function, so this acts as a flag to enable handling */
|
/** We can't unregister_shutdown_function, so this acts as a flag to enable handling */
|
||||||
protected $handleFatalErrors = false;
|
protected $handleFatalErrors = false;
|
||||||
|
|
||||||
|
/** We overload display_errors to hide errors during execution, so we need to remember the original to restore to */
|
||||||
|
protected $originalDisplayErrors = null;
|
||||||
|
|
||||||
public function hasErrored() {
|
public function hasErrored() {
|
||||||
return $this->error;
|
return $this->error;
|
||||||
}
|
}
|
||||||
@ -38,6 +38,7 @@ class ErrorControlChain {
|
|||||||
|
|
||||||
public function setSuppression($suppression) {
|
public function setSuppression($suppression) {
|
||||||
$this->suppression = (bool)$suppression;
|
$this->suppression = (bool)$suppression;
|
||||||
|
if ($this->handleFatalErrors) ini_set('display_errors', !$suppression);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,24 +69,14 @@ class ErrorControlChain {
|
|||||||
return $this->then($callback, null);
|
return $this->then($callback, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleError($errno, $errstr) {
|
|
||||||
if ((error_reporting() & self::$fatal_errors & $errno) != 0 && $this->suppression) {
|
|
||||||
throw new Exception('Generic Error');
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function lastErrorWasFatal() {
|
protected function lastErrorWasFatal() {
|
||||||
$error = error_get_last();
|
$error = error_get_last();
|
||||||
return $error && $error['type'] == 1;
|
return $error && ($error['type'] & self::$fatal_errors) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleFatalError() {
|
public function handleFatalError() {
|
||||||
if ($this->handleFatalErrors && $this->suppression) {
|
if ($this->handleFatalErrors && $this->suppression) {
|
||||||
if ($this->lastErrorWasFatal()) {
|
if ($this->lastErrorWasFatal()) {
|
||||||
ob_clean();
|
|
||||||
$this->error = true;
|
$this->error = true;
|
||||||
$this->step();
|
$this->step();
|
||||||
}
|
}
|
||||||
@ -93,10 +84,12 @@ class ErrorControlChain {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function execute() {
|
public function execute() {
|
||||||
set_error_handler(array($this, 'handleError'));
|
|
||||||
register_shutdown_function(array($this, 'handleFatalError'));
|
register_shutdown_function(array($this, 'handleFatalError'));
|
||||||
$this->handleFatalErrors = true;
|
$this->handleFatalErrors = true;
|
||||||
|
|
||||||
|
$this->originalDisplayErrors = ini_get('display_errors');
|
||||||
|
ini_set('display_errors', !$this->suppression);
|
||||||
|
|
||||||
$this->step();
|
$this->step();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,13 +98,7 @@ class ErrorControlChain {
|
|||||||
$step = array_shift($this->steps);
|
$step = array_shift($this->steps);
|
||||||
|
|
||||||
if ($step['onErrorState'] === null || $step['onErrorState'] === $this->error) {
|
if ($step['onErrorState'] === null || $step['onErrorState'] === $this->error) {
|
||||||
try {
|
call_user_func($step['callback'], $this);
|
||||||
call_user_func($step['callback'], $this);
|
|
||||||
}
|
|
||||||
catch (Exception $e) {
|
|
||||||
if ($this->suppression) $this->error = true;
|
|
||||||
else throw $e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->step();
|
$this->step();
|
||||||
@ -119,7 +106,7 @@ class ErrorControlChain {
|
|||||||
else {
|
else {
|
||||||
// Now clean up
|
// Now clean up
|
||||||
$this->handleFatalErrors = false;
|
$this->handleFatalErrors = false;
|
||||||
restore_error_handler();
|
ini_set('display_errors', $this->originalDisplayErrors);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user