|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/** |
| 3 | + * This file is part of toolkit/sys-utils. |
| 4 | + * |
| 5 | + * @author https://github.com/inhere |
| 6 | + * @link https://github.com/php-toolkit/sys-utils |
| 7 | + * @license MIT |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Toolkit\Stdlib\Util; |
| 11 | + |
| 12 | +use Throwable; |
| 13 | +use function get_class; |
| 14 | +use function json_encode; |
| 15 | +use function strip_tags; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class PhpException |
| 19 | + * |
| 20 | + * @package Toolkit\Stdlib\Util |
| 21 | + */ |
| 22 | +class PhpException |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @param Throwable $e |
| 26 | + * @param bool $getTrace |
| 27 | + * @param string|null $catcher |
| 28 | + * |
| 29 | + * @return string |
| 30 | + * @see PhpException::toHtml() |
| 31 | + */ |
| 32 | + public static function toText(Throwable $e, bool $getTrace = true, string $catcher = null): string |
| 33 | + { |
| 34 | + return self::toHtml($e, $getTrace, $catcher, true); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @param Throwable $e |
| 39 | + * @param bool $getTrace |
| 40 | + * @param string|null $catcher |
| 41 | + * |
| 42 | + * @return string |
| 43 | + * @see PhpException::toHtml() |
| 44 | + */ |
| 45 | + public static function toString(Throwable $e, bool $getTrace = true, string $catcher = null): string |
| 46 | + { |
| 47 | + return self::toHtml($e, $getTrace, $catcher, true); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Converts an exception into a simple string. |
| 52 | + * |
| 53 | + * @param Throwable $e the exception being converted |
| 54 | + * @param bool $clearHtml |
| 55 | + * @param bool $getTrace |
| 56 | + * @param null|string $catcher |
| 57 | + * |
| 58 | + * @return string the string representation of the exception. |
| 59 | + */ |
| 60 | + public static function toHtml(Throwable $e, bool $getTrace = true, string $catcher = null, bool $clearHtml = false): string |
| 61 | + { |
| 62 | + if (!$getTrace) { |
| 63 | + $message = "Error: {$e->getMessage()}"; |
| 64 | + } else { |
| 65 | + $message = sprintf( |
| 66 | + "<h3>%s(%d): %s</h3>\n<pre><strong>File: %s(Line %d)</strong>%s \n\n%s</pre>", |
| 67 | + get_class($e), |
| 68 | + $e->getCode(), |
| 69 | + $e->getMessage(), |
| 70 | + $e->getFile(), |
| 71 | + $e->getLine(), |
| 72 | + $catcher ? "\nCatch By: $catcher" : '', |
| 73 | + $e->getTraceAsString() |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + return $clearHtml ? strip_tags($message) : "<div class=\"exception-box\">$message</div>"; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Converts an exception into a simple array. |
| 82 | + * |
| 83 | + * @param Throwable $e the exception being converted |
| 84 | + * @param bool $getTrace |
| 85 | + * @param null|string $catcher |
| 86 | + * |
| 87 | + * @return array |
| 88 | + */ |
| 89 | + public static function toArray(Throwable $e, bool $getTrace = true, string $catcher = null): array |
| 90 | + { |
| 91 | + $data = [ |
| 92 | + 'class' => get_class($e), |
| 93 | + 'message' => $e->getMessage(), |
| 94 | + 'code' => $e->getCode(), |
| 95 | + 'file' => $e->getFile() . ':' . $e->getLine(), |
| 96 | + ]; |
| 97 | + |
| 98 | + if ($catcher) { |
| 99 | + $data['catcher'] = $catcher; |
| 100 | + } |
| 101 | + |
| 102 | + if ($getTrace) { |
| 103 | + $data['trace'] = $e->getTrace(); |
| 104 | + } |
| 105 | + |
| 106 | + return $data; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Converts an exception into a json string. |
| 111 | + * |
| 112 | + * @param Throwable $e the exception being converted |
| 113 | + * @param bool $getTrace |
| 114 | + * @param null|string $catcher |
| 115 | + * |
| 116 | + * @return string the string representation of the exception. |
| 117 | + */ |
| 118 | + public static function toJson(Throwable $e, bool $getTrace = true, string $catcher = null): string |
| 119 | + { |
| 120 | + $errMsg = $e->getMessage(); |
| 121 | + if (!$getTrace) { |
| 122 | + return json_encode(['msg' => "Error: $errMsg"]); |
| 123 | + } |
| 124 | + |
| 125 | + $map = [ |
| 126 | + 'code' => $e->getCode() ?: 500, |
| 127 | + 'msg' => sprintf( |
| 128 | + '%s(%d): %s, File: %s(Line %d)', |
| 129 | + get_class($e), |
| 130 | + $e->getCode(), |
| 131 | + $errMsg, |
| 132 | + $e->getFile(), |
| 133 | + $e->getLine() |
| 134 | + ), |
| 135 | + 'data' => $e->getTrace() |
| 136 | + ]; |
| 137 | + |
| 138 | + if ($catcher) { |
| 139 | + $map['catcher'] = $catcher; |
| 140 | + } |
| 141 | + |
| 142 | + $map['trace'] = $e->getTrace(); |
| 143 | + return json_encode($map); |
| 144 | + } |
| 145 | +} |
0 commit comments