|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: inhere |
| 5 | + * Date: 2017/10/9 |
| 6 | + * Time: 下午11:15 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace MyLib\PhpUtil; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class PhpException |
| 13 | + * @package MyLib\PhpUtil |
| 14 | + */ |
| 15 | +class PhpException |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @see PhpException::toHtml() |
| 19 | + * {@inheritdoc} |
| 20 | + */ |
| 21 | + public static function toString($e, $getTrace = true, $catcher = null): string |
| 22 | + { |
| 23 | + return self::toHtml($e, $getTrace, $catcher, true); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Converts an exception into a simple string. |
| 28 | + * @param \Exception|\Throwable $e the exception being converted |
| 29 | + * @param bool $clearHtml |
| 30 | + * @param bool $getTrace |
| 31 | + * @param null|string $catcher |
| 32 | + * @return string the string representation of the exception. |
| 33 | + */ |
| 34 | + public static function toHtml($e, $getTrace = true, $catcher = null, $clearHtml = false): string |
| 35 | + { |
| 36 | + if (!$getTrace) { |
| 37 | + $message = "Error: {$e->getMessage()}"; |
| 38 | + } else { |
| 39 | + $message = sprintf( |
| 40 | + "<h3>%s(%d): %s</h3>\n<pre><strong>File: %s(Line %d)</strong>%s \n\n%s</pre>", |
| 41 | + \get_class($e), |
| 42 | + $e->getCode(), |
| 43 | + $e->getMessage(), |
| 44 | + $e->getFile(), |
| 45 | + $e->getLine(), |
| 46 | + $catcher ? "\nCatch By: $catcher" : '', |
| 47 | + $e->getTraceAsString() |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + return $clearHtml ? strip_tags($message) : "<div class=\"exception-box\">{$message}</div>"; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Converts an exception into a simple array. |
| 56 | + * @param \Exception|\Throwable $e the exception being converted |
| 57 | + * @param bool $getTrace |
| 58 | + * @param null|string $catcher |
| 59 | + * @return array |
| 60 | + */ |
| 61 | + public static function toArray($e, $getTrace = true, $catcher = null) |
| 62 | + { |
| 63 | + $data = [ |
| 64 | + 'class' => \get_class($e), |
| 65 | + 'message' => $e->getMessage(), |
| 66 | + 'code' => $e->getCode(), |
| 67 | + 'file' => $e->getFile() . ':' . $e->getLine(), |
| 68 | + ]; |
| 69 | + |
| 70 | + if ($catcher) { |
| 71 | + $data['catcher'] = $catcher; |
| 72 | + } |
| 73 | + |
| 74 | + if ($getTrace) { |
| 75 | + $data['trace'] = $e->getTrace(); |
| 76 | + } |
| 77 | + |
| 78 | + return $data; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Converts an exception into a json string. |
| 83 | + * @param \Exception|\Throwable $e the exception being converted |
| 84 | + * @param bool $getTrace |
| 85 | + * @param null|string $catcher |
| 86 | + * @return string the string representation of the exception. |
| 87 | + */ |
| 88 | + public static function toJson($e, $getTrace = true, $catcher = null) |
| 89 | + { |
| 90 | + if (!$getTrace) { |
| 91 | + $message = json_encode(['msg' => "Error: {$e->getMessage()}"]); |
| 92 | + } else { |
| 93 | + $map = [ |
| 94 | + 'code' => $e->getCode() ?: 500, |
| 95 | + 'msg' => sprintf( |
| 96 | + '%s(%d): %s, File: %s(Line %d)', |
| 97 | + \get_class($e), |
| 98 | + $e->getCode(), |
| 99 | + $e->getMessage(), |
| 100 | + $e->getFile(), |
| 101 | + $e->getLine() |
| 102 | + ), |
| 103 | + 'data' => $e->getTrace() |
| 104 | + ]; |
| 105 | + |
| 106 | + if ($catcher) { |
| 107 | + $map['catcher'] = $catcher; |
| 108 | + } |
| 109 | + |
| 110 | + if ($getTrace) { |
| 111 | + $map['trace'] = $e->getTrace(); |
| 112 | + } |
| 113 | + |
| 114 | + $message = json_encode($map); |
| 115 | + } |
| 116 | + |
| 117 | + return $message; |
| 118 | + } |
| 119 | +} |
0 commit comments