| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 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 |
|
| 41 |
|
| 42 |
class ErrorHandler implements LoggerAwareInterface |
| 43 |
{ |
| 44 |
|
| 45 |
private $core = null; |
| 46 |
|
| 47 |
private $logger = null; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function __construct(object $core) |
| 55 |
{ |
| 56 |
$this->core = $core; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 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 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public static function exceptionResult(\Throwable $e): array |
| 90 |
{ |
| 91 |
return self::getResult($e, Core::instance()->config('debug', 0)); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public static function exceptionText(\Throwable $e): string |
| 103 |
{ |
| 104 |
return self::getText($e, Core::instance()->config('debug', 0)); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function setLogger(LoggerInterface $logger): void |
| 111 |
{ |
| 112 |
$this->logger = $logger; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 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 |
} |