Debian Codegraph @

classes/ErrorHandler.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2024 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This file contains ErrorHandler class
25 *
26 * @category Common
27 * @package DmarcSrg
28 * @author Aleksey Andreev (liuch)
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30 */
31
32 namespace Liuch\DmarcSrg;
33
34 use Liuch\DmarcSrg\Log\LoggerInterface;
35 use Liuch\DmarcSrg\Log\LoggerAwareInterface;
36 use Liuch\DmarcSrg\Exception\AuthException;
37 use Liuch\DmarcSrg\Exception\SoftException;
38
39 /**
40 * Uncaught exception handler
41 */
42 class ErrorHandler implements LoggerAwareInterface
43 {
44 /** @var Core */
45 private $core = null;
46 /** @var LoggerInterface|null */
47 private $logger = null;
48
49 /**
50 * The constructor
51 *
52 * @param Core $core
53 */
54 public function __construct(object $core)
55 {
56 $this->core = $core;
57 }
58
59 /**
60 * Handle uncaught exceptions. Used by set_exception_handler and set_error_handler functions
61 *
62 * @param \Throwable $e an exception to handle. For set_error_handler it is ErrorException.
63 *
64 * @return void
65 */
66 public function handleException(\Throwable $e): void
67 {
68 $debug = intval($this->core->config('debug', 0));
69
70 if ($this->logger) {
71 $this->logger->error(strval($e));
72 }
73
74 if (!Core::isWEB()) {
75 echo self::getText($e, $debug);
76 exit(1);
77 }
78 Core::sendJson(self::getResult($e, $debug));
79 }
80
81 /**
82 * Returns an result array based on the passed exception's data.
83 * If the debug mode is enabled, the `debug_info` field will be added to the result.
84 *
85 * @param \Throwable $e an exception for which the result is generated
86 *
87 * @return array
88 */
89 public static function exceptionResult(\Throwable $e): array
90 {
91 return self::getResult($e, Core::instance()->config('debug', 0));
92 }
93
94 /**
95 * Returns information about the passed exception as text.
96 * If the debug is enabled, debug information will be added.
97 *
98 * @param \Throwable $e an exception for which the text is generated
99 *
100 * @return string
101 */
102 public static function exceptionText(\Throwable $e): string
103 {
104 return self::getText($e, Core::instance()->config('debug', 0));
105 }
106
107 /**
108 * Sets a logger to log uncaught exceptions and errors
109 */
110 public function setLogger(LoggerInterface $logger): void
111 {
112 $this->logger = $logger;
113 }
114
115 /**
116 * Returns the current logger
117 *
118 * @return LoggerInterface|null
119 */
120 public function logger()
121 {
122 return $this->logger;
123 }
124
125 private static function getResult(\Throwable $e, int $debug): array
126 {
127 $code = $e->getCode();
128 if ($code === 0) {
129 $code = -1;
130 }
131 $res = [
132 'error_code' => $code,
133 'message' => $e->getMessage()
134 ];
135 if ($code === -2 && $e instanceof AuthException) {
136 $res['authenticated'] = 'no';
137 $res['auth_type'] = $e->getAuthType();
138 }
139 if ($debug &&
140 (Core::instance()->getCurrentUser() || php_sapi_name() === 'cli') &&
141 !($e instanceof SoftException)
142 ) {
143 $prev = $e->getPrevious();
144 $res['debug_info'] = [
145 'code' => ($prev ?? $e)->getCode(),
146 'content' => strval($prev ?? $e)
147 ];
148 }
149 return $res;
150 }
151
152 private static function getText(\Throwable $e, int $debug): string
153 {
154 $msg = 'Error: ' . $e->getMessage() . ' (' . $e->getCode() . ')' . PHP_EOL;
155 if (!$debug) {
156 return $msg;
157 }
158
159 return '-----' . PHP_EOL
160 . $msg
161 . '-----' . PHP_EOL
162 . $e . PHP_EOL;
163 }
164 }