hyperf-chat/app/Exception/Handler/BaseExceptionHandler.php

38 lines
991 B
PHP
Raw Normal View History

2021-07-16 22:15:49 +08:00
<?php
declare(strict_types=1);
namespace App\Exception\Handler;
use App\Exception\BaseException;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\ResponseInterface;
use Throwable;
/**
* Class BaseExceptionHandler
*
* @package App\Exception\Handler
*/
class BaseExceptionHandler extends ExceptionHandler
{
public function handle(Throwable $throwable, ResponseInterface $response)
{
// 格式化输出
$data = json_encode([
'code' => $throwable->getCode(),
'message' => $throwable->getMessage(),
], JSON_UNESCAPED_UNICODE);
// 阻止异常冒泡
$this->stopPropagation();
return $response->withAddedHeader('content-type', 'application/json; charset=utf-8')->withStatus(200)->withBody(new SwooleStream($data));
}
public function isValid(Throwable $throwable): bool
{
return $throwable instanceof BaseException;
}
}