| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\Exceptions; |
| 4 |
|
| 5 |
use Error; |
| 6 |
use Exception; |
| 7 |
use Give\Framework\Exceptions\Contracts\LoggableException; |
| 8 |
use Give\Framework\Exceptions\Traits\Loggable; |
| 9 |
use Give\Log\Log; |
| 10 |
|
| 11 |
class UncaughtExceptionLogger |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @var callable|null |
| 15 |
*/ |
| 16 |
private $previousHandler; |
| 17 |
|
| 18 |
/** |
| 19 |
* Registers the class with the set_exception_handler to receive uncaught exceptions |
| 20 |
* |
| 21 |
* @since 2.11.1 |
| 22 |
*/ |
| 23 |
public function setupExceptionHandler() |
| 24 |
{ |
| 25 |
if ($this->previousHandler !== null) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
$this->previousHandler = @set_exception_handler([$this, 'handleException']); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Handles an uncaught exception by checking if the Exception is native to GiveWP and then logging it if it is |
| 34 |
* |
| 35 |
* @since 2.12.0 re-throw the exception so it displays |
| 36 |
* @since 2.11.2 remove parameter typing as it may be an Error |
| 37 |
* @since 2.11.1 |
| 38 |
* |
| 39 |
* @param Exception|Error $exception |
| 40 |
* |
| 41 |
* @throws LoggableException |
| 42 |
*/ |
| 43 |
public function handleException($exception) |
| 44 |
{ |
| 45 |
if ($exception instanceof LoggableException) { |
| 46 |
/** @var LoggableException|Loggable $exception */ |
| 47 |
Log::error($exception->getLogMessage(), $exception->getLogContext()); |
| 48 |
} |
| 49 |
|
| 50 |
if ($this->previousHandler !== null) { |
| 51 |
$previousHandler = $this->previousHandler; |
| 52 |
$previousHandler($exception); |
| 53 |
} else { |
| 54 |
throw $exception; |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|