logger = $logger; } public function handle(Throwable $throwable, ResponseInterface $response) { // 如果是授权异常 if ($throwable instanceof TokenValidException || $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->withHeader('Server', 'hcbet') ->withHeader('Content-Type','application/json') ->withStatus(401) ->withBody(new SwooleStream($data)); } $this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile())); $this->logger->error($throwable->getTraceAsString()); return $response->withHeader('Server', 'hcbet')->withStatus(500)->withBody(new SwooleStream('Internal Server Error.')); } public function isValid(Throwable $throwable): bool { return true; } }