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

56 lines
1.5 KiB
PHP
Raw Normal View History

2020-11-02 22:45:37 +08:00
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Exception\Handler;
2020-11-04 11:57:16 +08:00
use App\Exception\ValidateException;
2020-11-02 22:45:37 +08:00
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\ResponseInterface;
use Throwable;
2020-11-04 16:47:17 +08:00
use Phper666\JWTAuth\Exception\TokenValidException;
2020-11-02 22:45:37 +08:00
class AppExceptionHandler extends ExceptionHandler
{
/**
* @var StdoutLoggerInterface
*/
protected $logger;
public function __construct(StdoutLoggerInterface $logger)
{
$this->logger = $logger;
}
public function handle(Throwable $throwable, ResponseInterface $response)
{
2020-11-04 11:57:16 +08:00
// 判断是否是验证器异常类
if($throwable instanceof ValidateException){
return $response;
}
2020-11-04 16:47:17 +08:00
if($throwable instanceof TokenValidException){
return $response;
}
2020-11-02 22:45:37 +08:00
$this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
$this->logger->error($throwable->getTraceAsString());
2020-11-04 11:57:16 +08:00
2020-11-02 22:45:37 +08:00
return $response->withHeader('Server', 'Hyperf')->withStatus(500)->withBody(new SwooleStream('Internal Server Error.'));
}
public function isValid(Throwable $throwable): bool
{
return true;
}
}