server/app/Exception/Handler/DbQueryExceptionHandle.php

43 lines
1.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Author: cfn <cfn@leapy.cn>
*/
namespace App\Exception\Handler;
use App\Service\Log;
use App\Service\QueueService;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\ResponseInterface;
use Throwable;
use function Hyperf\Support\env;
/**
*
* Author: cfn <cfn@leapy.cn>
*/
class DbQueryExceptionHandle extends ExceptionHandler
{
public function __construct(protected StdoutLoggerInterface $logger)
{
}
/**
* Author: cfn <cfn@leapy.cn>
* @param Throwable $throwable
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function handle(Throwable $throwable, ResponseInterface $response)
{
return $response->withHeader('Server', 'leapy')->withStatus(500)->withBody(new SwooleStream(json_encode(['code'=>2,'msg'=>"SQL语句存在错误请联系服务商"])));
}
public function isValid(Throwable $throwable): bool
{
return $throwable instanceof \Hyperf\Database\Exception\QueryException;
}
}