logger = $logger; } public function handle(Throwable $throwable, ResponseInterface $response): MessageInterface|ResponseInterface { if ($throwable instanceof ValidationFailedException) { return $response->withBody(new SwooleStream($throwable->getMessage())); } // 处理业务异常 if ($throwable instanceof BusinessException) { $data = [ Config::RESPONSE_FIELD_KEY_CODE => $throwable->getCode(), Config::RESPONSE_FIELD_KEY_MESSAGE => $throwable->getMessage(), Config::RESPONSE_FIELD_KEY_DATA => null, ]; } // 数据库无记录异常 if ($throwable instanceof ModelNotFoundException) { $data = [ Config::RESPONSE_FIELD_KEY_CODE => 404, Config::RESPONSE_FIELD_KEY_MESSAGE => $throwable->getMessage() ?: '没有对应记录', Config::RESPONSE_FIELD_KEY_DATA => null, ]; } else { // 记录未知错误日志 $this->logger->error(sprintf( 'Unknown error: %s %s in %s:%d', $throwable->getMessage(), $throwable->getCode(), $throwable->getFile(), $throwable->getLine() )); $data = [ Config::RESPONSE_FIELD_KEY_CODE => 500, Config::RESPONSE_FIELD_KEY_MESSAGE => 'Server internal error', Config::RESPONSE_FIELD_KEY_DATA => env('APP_ENV') === 'dev' ? [ 'message' => $throwable->getMessage(), 'file' => $throwable->getFile(), 'line' => $throwable->getLine(), 'trace' => $throwable->getTraceAsString(), ] : null, ]; } $body = json_encode($data, JSON_UNESCAPED_UNICODE); return $response->withHeader('Content-Type', 'application/json') ->withBody(new SwooleStream($body)); } public function isValid(Throwable $throwable): bool { return true; } }