You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.4 KiB
56 lines
1.4 KiB
<?php
|
|
|
|
namespace App\Exception\Handler;
|
|
|
|
use App\Commons\Log;
|
|
use App\Constants\v3\LogLabel;
|
|
use App\Exception\ErrorCodeException;
|
|
use Hyperf\Contract\StdoutLoggerInterface;
|
|
use Hyperf\ExceptionHandler\ExceptionHandler;
|
|
use Hyperf\HttpMessage\Stream\SwooleStream;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Throwable;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
class ErrorCodeExceptionHandler extends ExceptionHandler
|
|
{
|
|
/**
|
|
* @Inject
|
|
* @var Log
|
|
*/
|
|
protected $log;
|
|
|
|
/**
|
|
* @var StdoutLoggerInterface
|
|
*/
|
|
protected $logger;
|
|
|
|
public function __construct(StdoutLoggerInterface $logger)
|
|
{
|
|
$this->logger = $logger;
|
|
}
|
|
|
|
public function handle(Throwable $throwable, ResponseInterface $response)
|
|
{
|
|
$this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
|
|
$this->logger->error($throwable->getTraceAsString());
|
|
|
|
$this->stopPropagation();
|
|
|
|
$content = [
|
|
"status" => 'error',
|
|
"code" => $throwable->getCode(),
|
|
"result" => [],
|
|
"message" => $throwable->getMessage()
|
|
];
|
|
|
|
return $response->withHeader('Content-Type', 'application/json')
|
|
->withStatus(200)
|
|
->withBody(new SwooleStream(json_encode($content)));
|
|
}
|
|
|
|
public function isValid(Throwable $throwable): bool
|
|
{
|
|
return $throwable instanceof ErrorCodeException;
|
|
}
|
|
}
|