Skip to content

Commit

Permalink
enhance(exceptions): Add shouldIgnore and shouldntIgnore methods …
Browse files Browse the repository at this point in the history
…for ignoring error types (#58)

* enhance(exceptions): Ignore `E_USER_DEPRECATED` errors since WordPress is the definition of deprecated
* chore(exceptions): Remove the `error_reporting` directive set by Acorn
  • Loading branch information
Log1x authored Apr 8, 2020
1 parent 3176bca commit 7056a80
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions src/Acorn/Bootstrap/HandleExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Arr;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\ErrorHandler\Error\FatalError;
use Throwable;
Expand All @@ -26,6 +27,13 @@ class HandleExceptions
*/
protected $app;

/**
* A list of the error types that are ignored.
*
* @var array
*/
protected $ignoredErrors = ['E_USER_DEPRECATED'];

/**
* Bootstrap the given application.
*
Expand All @@ -42,8 +50,6 @@ public function bootstrap(Application $app)
return;
}

error_reporting(-1 & ~E_USER_NOTICE);

set_error_handler([$this, 'handleError']);

set_exception_handler([$this, 'handleException']);
Expand Down Expand Up @@ -71,7 +77,14 @@ public function bootstrap(Application $app)
public function handleError($level, $message, $file = '', $line = 0, $context = [])
{
if (error_reporting() & $level) {
throw new ErrorException($message, 0, $level, $file, $line);
if (
$this->shouldIgnore($e = new ErrorException($message, 0, $level, $file, $line)) &&
! $this->app->runningInConsole()
) {
return;
}

throw $e;
}
}

Expand Down Expand Up @@ -159,6 +172,30 @@ protected function isFatal($type)
return in_array($type, [E_COMPILE_ERROR, E_CORE_ERROR, E_ERROR, E_PARSE]);
}

/**
* Determine if the error type should be ignored.
*
* @param \Throwable $e
* @return bool
*/
public function shouldIgnore(Throwable $e)
{
return ! $this->shouldntIgnore($e);
}

/**
* Determine if the exception is in the ignore list.
*
* @param \Throwable $e
* @return bool
*/
protected function shouldntIgnore(Throwable $e)
{
return ! is_null(Arr::first($this->ignoredErrors, function ($type) use ($e) {
return $e instanceof $type;
}));
}

/**
* Get an instance of the exception handler.
*
Expand Down

0 comments on commit 7056a80

Please # to comment.