Skip to content

prepend exception handler #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 93 additions & 42 deletions src/Weew/ErrorHandler/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
use Weew\ErrorHandler\Errors\IError;
use Weew\ErrorHandler\Exceptions\InvalidHandlerType;
use Weew\ErrorHandler\Handlers\ExceptionHandler;
use Weew\ErrorHandler\Handlers\NativeErrorHandler;
use Weew\ErrorHandler\Handlers\IExceptionHandler;
use Weew\ErrorHandler\Handlers\INativeErrorHandler;
use Weew\ErrorHandler\Handlers\NativeErrorHandler;

class ErrorHandler implements IErrorHandler {
class ErrorHandler implements IErrorHandler
{
/**
* @var ErrorConverter
*/
Expand Down Expand Up @@ -62,23 +63,26 @@ class ErrorHandler implements IErrorHandler {
*
* @param bool $convertErrorsToExceptions
*/
public function __construct($convertErrorsToExceptions = false) {
public function __construct($convertErrorsToExceptions = false)
{
$this->errorConverter = $this->createErrorConverter();
$this->convertErrorsToExceptions($convertErrorsToExceptions);
}

/**
* Enable exception, error and fatal error handling.
*/
public function enable() {
public function enable()
{
$this->enableExceptionHandling();
$this->enableErrorHandling();
}

/**
* Enable exception handling.
*/
public function enableExceptionHandling() {
public function enableExceptionHandling()
{
if ($this->isExceptionHandlingEnabled()) {
return;
}
Expand All @@ -90,15 +94,17 @@ public function enableExceptionHandling() {
/**
* Enable handling of native PHP errors.
*/
public function enableErrorHandling() {
public function enableErrorHandling()
{
$this->enableRecoverableErrorHandling();
$this->enableFatalErrorHandling();
}

/**
* Enable regular error handling.
*/
public function enableRecoverableErrorHandling() {
public function enableRecoverableErrorHandling()
{
if ($this->isRecoverableErrorHandlingEnabled()) {
return;
}
Expand All @@ -117,7 +123,8 @@ public function enableRecoverableErrorHandling() {
/**
* Enable fatal/non-recoverable error handling.
*/
public function enableFatalErrorHandling() {
public function enableFatalErrorHandling()
{
if ($this->isFatalErrorHandlingEnabled()) {
return;
}
Expand All @@ -132,36 +139,41 @@ public function enableFatalErrorHandling() {
/**
* @return bool
*/
public function isExceptionHandlingEnabled() {
public function isExceptionHandlingEnabled()
{
return $this->isExceptionHandlingEnabled;
}

/**
* @return bool
*/
public function isErrorHandlingEnabled() {
public function isErrorHandlingEnabled()
{
return $this->isRecoverableErrorHandlingEnabled()
&& $this->isFatalErrorHandlingEnabled();
&& $this->isFatalErrorHandlingEnabled();
}

/**
* @return bool
*/
public function isRecoverableErrorHandlingEnabled() {
public function isRecoverableErrorHandlingEnabled()
{
return $this->isRecoverableErrorHandlingEnabled;
}

/**
* @return bool
*/
public function isFatalErrorHandlingEnabled() {
public function isFatalErrorHandlingEnabled()
{
return $this->isFatalErrorHandlingEnabled;
}

/**
* @return bool
*/
public function isConvertingErrorsToExceptions() {
public function isConvertingErrorsToExceptions()
{
return $this->isConvertingErrorsToExceptions;
}

Expand All @@ -172,29 +184,35 @@ public function isConvertingErrorsToExceptions() {
*
* @throws InvalidHandlerType
*/
public function addExceptionHandler($handler) {
if ( ! $handler instanceof IExceptionHandler && ! is_callable($handler)) {
throw new InvalidHandlerType(
s('Exception handler must be a callable or an instance of %s.',
IExceptionHandler::class)
);
}

if (is_callable($handler)) {
$handler = $this->createExceptionHandler($handler);
}
public function addExceptionHandler($handler)
{
$handler = $this->createHandlerForException($handler);

$this->exceptionHandlers[] = $handler;
}

/**
* Prepend an error handler for exceptions.
*
* @param callable|IExceptionHandler $handler
*
* @throws InvalidHandlerType
*/
public function prependExceptionHandler($handler)
{
$handler = $this->createHandlerForException($handler);
array_unshift($this->exceptionHandlers, $handler);
}

/**
* Add an error handler for all kinds of native PHP errors.
*
* @param callable|INativeErrorHandler $handler
*
* @throws InvalidHandlerType
*/
public function addErrorHandler($handler) {
public function addErrorHandler($handler)
{
$this->addRecoverableErrorHandler($handler);
$this->addFatalErrorHandler($handler);
}
Expand All @@ -206,8 +224,9 @@ public function addErrorHandler($handler) {
*
* @throws InvalidHandlerType
*/
public function addRecoverableErrorHandler($handler) {
if ( ! $handler instanceof INativeErrorHandler && ! is_callable($handler)) {
public function addRecoverableErrorHandler($handler)
{
if (!$handler instanceof INativeErrorHandler && !is_callable($handler)) {
throw new InvalidHandlerType(
s('Recoverable error handler must be a callable or an instance of %s.',
INativeErrorHandler::class)
Expand All @@ -228,8 +247,9 @@ public function addRecoverableErrorHandler($handler) {
*
* @throws InvalidHandlerType
*/
public function addFatalErrorHandler($handler) {
if ( ! $handler instanceof INativeErrorHandler && ! is_callable($handler)) {
public function addFatalErrorHandler($handler)
{
if (!$handler instanceof INativeErrorHandler && !is_callable($handler)) {
throw new InvalidHandlerType(
s('Fatal error handler must be a callable or an instance of %s.',
INativeErrorHandler::class)
Expand All @@ -248,11 +268,12 @@ public function addFatalErrorHandler($handler) {
*
* @throws Throwable
*/
public function handleException(Throwable $ex) {
public function handleException(Throwable $ex)
{
foreach ($this->getExceptionHandlers() as $handler) {
if ($handler->isEnabled()) {
try {
if ( ! $handler->supports($ex)) {
if (!$handler->supports($ex)) {
continue;
}

Expand All @@ -278,7 +299,8 @@ public function handleException(Throwable $ex) {
/**
* @param IError $error
*/
public function handleError(IError $error) {
public function handleError(IError $error)
{
if ($error->isRecoverable()) {
$this->handleRecoverableError($error);
} else {
Expand All @@ -291,7 +313,8 @@ public function handleError(IError $error) {
*
* @return bool|void
*/
public function handleRecoverableError(IError $error) {
public function handleRecoverableError(IError $error)
{
// ignore error caused by a rethrown exception
if ($this->ignoreRethrownException) {
$this->ignoreRethrownException = false;
Expand Down Expand Up @@ -326,7 +349,8 @@ public function handleRecoverableError(IError $error) {
*
* @return bool|void
*/
public function handleFatalError(IError $error) {
public function handleFatalError(IError $error)
{
// ignore error caused by a rethrown exception
if ($this->ignoreRethrownException) {
$this->ignoreRethrownException = false;
Expand Down Expand Up @@ -361,7 +385,8 @@ public function handleFatalError(IError $error) {
/**
* @param bool $convertErrorsToExceptions
*/
public function convertErrorsToExceptions($convertErrorsToExceptions) {
public function convertErrorsToExceptions($convertErrorsToExceptions)
{
$this->isConvertingErrorsToExceptions = $convertErrorsToExceptions;

if ($convertErrorsToExceptions) {
Expand All @@ -372,21 +397,24 @@ public function convertErrorsToExceptions($convertErrorsToExceptions) {
/**
* @return IExceptionHandler[]
*/
public function getExceptionHandlers() {
public function getExceptionHandlers()
{
return $this->exceptionHandlers;
}

/**
* @return INativeErrorHandler[]
*/
public function getRecoverableErrorHandlers() {
public function getRecoverableErrorHandlers()
{
return $this->recoverableErrorHandlers;
}

/**
* @return INativeErrorHandler[]
*/
public function getFatalErrorHandlers() {
public function getFatalErrorHandlers()
{
return $this->fatalErrorHandlers;
}

Expand All @@ -395,7 +423,8 @@ public function getFatalErrorHandlers() {
*
* @return IExceptionHandler
*/
protected function createExceptionHandler(callable $handler) {
protected function createExceptionHandler(callable $handler)
{
return new ExceptionHandler($handler);
}

Expand All @@ -404,14 +433,36 @@ protected function createExceptionHandler(callable $handler) {
*
* @return INativeErrorHandler
*/
protected function createNativeErrorHandler(callable $handler) {
protected function createNativeErrorHandler(callable $handler)
{
return new NativeErrorHandler($handler);
}

/**
* @return ErrorConverter
*/
protected function createErrorConverter() {
protected function createErrorConverter()
{
return new ErrorConverter();
}

/**
* @param $handler
* @return callable|ExceptionHandler|IExceptionHandler
* @throws InvalidHandlerType
*/
private function createHandlerForException($handler)
{
if (!$handler instanceof IExceptionHandler && !is_callable($handler)) {
throw new InvalidHandlerType(
s('Exception handler must be a callable or an instance of %s.',
IExceptionHandler::class)
);
}

if (is_callable($handler)) {
$handler = $this->createExceptionHandler($handler);
}
return $handler;
}
}
7 changes: 7 additions & 0 deletions src/Weew/ErrorHandler/IErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ function isConvertingErrorsToExceptions();
*/
function addExceptionHandler($handler);

/**
* Prepend an error handler for exceptions.
*
* @param callable|IExceptionHandler $handler
*/
function prependExceptionHandler($handler);

/**
* Add an error handler for all kinds of native PHP errors.
*
Expand Down
Loading