logger = $logger; } public function handle(Throwable $throwable, ResponseInterface $response) { // 判断被捕获到的异常是希望被捕获的异常 if ($throwable instanceof TokenValidException) { // 格式化输出 $message = $throwable->getMessage(); $message = is_numeric($message)? __('api.'.$message) : $message; $data = json_encode([ 'code' => __('api.'.$throwable->getCode()), 'message' => $message ], 256); // 阻止异常冒泡 $this->stopPropagation(); return $response->withStatus(500)->withBody(new SwooleStream($data)); } // 交给下一个异常处理器 return $response; } public function isValid(Throwable $throwable): bool { return true; } }