PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 2.19.6
GiveWP – Donation Plugin and Fundraising Platform v2.19.6
4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 All 253 releases
give / src / Framework / Exceptions / UncaughtExceptionLogger.php
UncaughtExceptionLogger.php
58 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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