Skip to content

Commit 7056a80

Browse files
authored
enhance(exceptions): Add shouldIgnore and shouldntIgnore methods 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
1 parent 3176bca commit 7056a80

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

src/Acorn/Bootstrap/HandleExceptions.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Exception;
77
use Illuminate\Contracts\Debug\ExceptionHandler;
88
use Illuminate\Contracts\Foundation\Application;
9+
use Illuminate\Support\Arr;
910
use Symfony\Component\Console\Output\ConsoleOutput;
1011
use Symfony\Component\ErrorHandler\Error\FatalError;
1112
use Throwable;
@@ -26,6 +27,13 @@ class HandleExceptions
2627
*/
2728
protected $app;
2829

30+
/**
31+
* A list of the error types that are ignored.
32+
*
33+
* @var array
34+
*/
35+
protected $ignoredErrors = ['E_USER_DEPRECATED'];
36+
2937
/**
3038
* Bootstrap the given application.
3139
*
@@ -42,8 +50,6 @@ public function bootstrap(Application $app)
4250
return;
4351
}
4452

45-
error_reporting(-1 & ~E_USER_NOTICE);
46-
4753
set_error_handler([$this, 'handleError']);
4854

4955
set_exception_handler([$this, 'handleException']);
@@ -71,7 +77,14 @@ public function bootstrap(Application $app)
7177
public function handleError($level, $message, $file = '', $line = 0, $context = [])
7278
{
7379
if (error_reporting() & $level) {
74-
throw new ErrorException($message, 0, $level, $file, $line);
80+
if (
81+
$this->shouldIgnore($e = new ErrorException($message, 0, $level, $file, $line)) &&
82+
! $this->app->runningInConsole()
83+
) {
84+
return;
85+
}
86+
87+
throw $e;
7588
}
7689
}
7790

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

175+
/**
176+
* Determine if the error type should be ignored.
177+
*
178+
* @param \Throwable $e
179+
* @return bool
180+
*/
181+
public function shouldIgnore(Throwable $e)
182+
{
183+
return ! $this->shouldntIgnore($e);
184+
}
185+
186+
/**
187+
* Determine if the exception is in the ignore list.
188+
*
189+
* @param \Throwable $e
190+
* @return bool
191+
*/
192+
protected function shouldntIgnore(Throwable $e)
193+
{
194+
return ! is_null(Arr::first($this->ignoredErrors, function ($type) use ($e) {
195+
return $e instanceof $type;
196+
}));
197+
}
198+
162199
/**
163200
* Get an instance of the exception handler.
164201
*

0 commit comments

Comments
 (0)