45 lines
1.2 KiB
PHP
45 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Exception\Handler;
|
|
|
|
use App\Exception\AdminException;
|
|
use App\Lib\AdminReturn;
|
|
use Hyperf\ExceptionHandler\ExceptionHandler;
|
|
use Hyperf\HttpMessage\Stream\SwooleStream;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Throwable;
|
|
|
|
class AdminExceptionHandler extends ExceptionHandler
|
|
{
|
|
public function __construct(protected AdminReturn $return)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* admin控制器异常处理
|
|
* @param Throwable $throwable
|
|
* @param ResponseInterface $response
|
|
* @return ResponseInterface
|
|
*/
|
|
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
|
|
{
|
|
if ($throwable instanceof AdminException) {
|
|
$result = $this->return->error($throwable->getMessage(),[],$throwable->getCode());
|
|
|
|
// 阻止异常冒泡
|
|
$this->stopPropagation();
|
|
|
|
return $response->withHeader("Content-Type", "application/json")
|
|
->withStatus(200)
|
|
->withBody(new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
|
}
|
|
|
|
// 交给下一个异常处理器
|
|
return $response;
|
|
}
|
|
|
|
public function isValid(Throwable $throwable): bool
|
|
{
|
|
return true;
|
|
}
|
|
} |