logger = $logger; $this->formatter = $formatter; } public function handle(Throwable $throwable, ResponseInterface $response) { $message = $throwable->getMessage(); $arrSrace = $throwable->getTrace(); $strTrace = $throwable->getTraceAsString(); $code = $throwable->getCode(); $this->logger->error(sprintf('%s[%s] in %s', $message, $throwable->getLine(), $throwable->getFile())); $this->logger->error($strTrace); $clientRequest = request(); $headerContentType = $clientRequest->header('content-type'); if (!empty($headerContentType) && Str::contains($headerContentType, ['/json', '+json'])) { // 是Json请求. // 格式化输出. $data = [ 'code' => $code, 'array_trace' => $arrSrace, 'string_trace' => $strTrace, ]; if (env('APP_ENV', 'prod') == 'prod') { $message = '内部错误'; $data = []; } return json_fail($message, 500, $data); } else { return $response->withStatus(500)->withBody(new SwooleStream('内部错误.')); } } public function isValid(Throwable $throwable): bool { return true; } }