handle()
handle(\Throwable $throwable, \Psr\Http\Message\ResponseInterface $response)
Parameters
\Throwable | $throwable | |
\Psr\Http\Message\ResponseInterface | $response |
Class TokenExceptionHandler
<?php
/**
* MineAdmin is committed to providing solutions for quickly building web applications
* Please view the LICENSE file that was distributed with this source code,
* For the full copyright and license information.
* Thank you very much for using MineAdmin.
*
* @Author X.Mo<root@imoi.cn>
* @Link https://gitee.com/xmo/MineAdmin
*/
declare(strict_types=1);
namespace Mine\Exception\Handler;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Utils\Codec\Json;
use Mine\Exception\NoPermissionException;
use Mine\Helper\MineCode;
use Psr\Http\Message\ResponseInterface;
use Throwable;
/**
* Class TokenExceptionHandler
* @package Mine\Exception\Handler
*/
class NoPermissionExceptionHandler extends ExceptionHandler
{
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
{
$this->stopPropagation();
$format = [
'success' => false,
'message' => $throwable->getMessage(),
'code' => MineCode::NO_PERMISSION,
];
return $response->withHeader('Server', 'MineAdmin')
->withAddedHeader('content-type', 'application/json; charset=utf-8')
->withStatus(403)->withBody(new SwooleStream(Json::encode($format)));
}
public function isValid(Throwable $throwable): bool
{
return $throwable instanceof NoPermissionException;
}
}