@@ -48,6 +48,111 @@ Later, I also wrote a small log receiving script. Combined with this component,
48
48
49
49
👉 [ oh-shit-logger] ( https://github.com/zxc7563598/oh-shit-logger )
50
50
51
+ > Corresponding complete custom error handling (from webman)
52
+
53
+ ``` php
54
+ <?php
55
+
56
+ namespace app\exception;
57
+
58
+ use Carbon\Carbon;
59
+ use Throwable;
60
+ use Webman\Exception\ExceptionHandler;
61
+ use Webman\Http\Request;
62
+ use Webman\Http\Response;
63
+ use support\exception\BusinessException;
64
+ use Hejunjie\ErrorLog\Logger;
65
+ use Hejunjie\ErrorLog\Handlers;
66
+
67
+ /**
68
+ * Class Handler
69
+ * @package support\exception
70
+ */
71
+ class Handler extends ExceptionHandler
72
+ {
73
+ public $dontReport = [
74
+ BusinessException::class,
75
+ ];
76
+
77
+ public function report(Throwable $exception)
78
+ {
79
+ parent::report($exception);
80
+ if ($this->shouldntReport($exception)) {
81
+ return;
82
+ }
83
+ $request = request();
84
+ $date = Carbon::now()->timezone(config('app')['default_timezone'])->format('Y-m-d');
85
+ (new Logger([
86
+ new Handlers\FileHandler(runtime_path("logs/{$date}/critical")),
87
+ new Handlers\RemoteApiHandler(config('app')['log_report_url'])
88
+ ]))->error(get_class($exception), $exception->getMessage(), [
89
+ 'project' => config('app')['app_name'],
90
+ 'ip' => $request->getRealIp(),
91
+ 'method' => $request->method(),
92
+ 'full_url' => $request->fullUrl(),
93
+ 'trace' => $this->getDebugData($exception)
94
+ ]);
95
+ }
96
+
97
+ public function render(Request $request, Throwable $exception): Response
98
+ {
99
+ $isDebug = config('app')['debug'] == 1;
100
+ $statusCode = $this->getHttpStatusCode($exception);
101
+ $response = [
102
+ 'code' => $this->getErrorCode($exception),
103
+ 'message' => $isDebug ? $exception->getMessage() : 'Server Error',
104
+ 'data' => $isDebug ? $this->getDebugData($exception) : new \stdClass()
105
+ ];
106
+ if ($requestId = $request->header('X-Request-ID')) {
107
+ $response['request_id'] = $requestId;
108
+ }
109
+ return new Response(
110
+ $statusCode,
111
+ ['Content-Type' => 'application/json'],
112
+ json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
113
+ );
114
+ }
115
+
116
+ protected function getHttpStatusCode(Throwable $exception): int
117
+ {
118
+ $code = $exception->getCode();
119
+ return ($code >= 100 && $code < 600) ? $code : 500;
120
+ }
121
+
122
+ protected function getErrorCode(Throwable $exception): int
123
+ {
124
+ return $exception->getCode() ?: 500;
125
+ }
126
+
127
+ protected function getDebugData(Throwable $exception): array
128
+ {
129
+ $trace = $exception->getTrace();
130
+ $simplifiedTrace = array_map(function ($frame) {
131
+ return [
132
+ 'file' => $frame['file'] ?? '[internal function]',
133
+ 'line' => $frame['line'] ?? 0,
134
+ 'function' => $frame['function'] ?? null,
135
+ 'class' => $frame['class'] ?? null,
136
+ 'type' => $frame['type'] ?? null
137
+ ];
138
+ }, $trace);
139
+ return [
140
+ 'class' => get_class($exception),
141
+ 'message' => $exception->getMessage(),
142
+ 'code' => $exception->getCode(),
143
+ 'file' => $exception->getFile(),
144
+ 'line' => $exception->getLine(),
145
+ 'trace' => config('app')['debug'] == 1 ? $simplifiedTrace : array_slice($simplifiedTrace, 0, 5),
146
+ 'previous' => $exception->getPrevious() ? [
147
+ 'class' => get_class($exception->getPrevious()),
148
+ 'message' => $exception->getPrevious()->getMessage()
149
+ ] : null
150
+ ];
151
+ }
152
+ }
153
+
154
+ ```
155
+
51
156
## 🔧 Additional Toolkits (Can be used independently or installed together)
52
157
53
158
This project was originally extracted from [ hejunjie/tools] ( https://github.com/zxc7563598/php-tools ) .
0 commit comments