*/ namespace App\Exception\Handler; use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\ExceptionHandler\ExceptionHandler; use Hyperf\HttpMessage\Stream\SwooleStream; use Psr\Http\Message\ResponseInterface; use Throwable; class RateLimitExceptionHandle extends ExceptionHandler { public function __construct(protected StdoutLoggerInterface $logger) { } public function handle(Throwable $throwable, ResponseInterface $response) { $this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile())); $this->logger->error($throwable->getTraceAsString()); return $response->withHeader('Server', 'leapy')->withStatus(500)->withBody(new SwooleStream(json_encode(['code'=>2,'msg'=>"触发服务器限流,请稍后重试"]))); } public function isValid(Throwable $throwable): bool { return $throwable instanceof \Hyperf\RateLimit\Exception\RateLimitException; } }