48 lines
1.2 KiB
PHP
48 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Exception\Handler;
|
|
|
|
use App\Constants\ResponseCode;
|
|
use Hyperf\Contract\StdoutLoggerInterface;
|
|
use Hyperf\ExceptionHandler\ExceptionHandler;
|
|
use Hyperf\HttpMessage\Stream\SwooleStream;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Throwable;
|
|
|
|
class AppExceptionHandler extends ExceptionHandler
|
|
{
|
|
/**
|
|
* @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());
|
|
|
|
$data = json_encode([
|
|
'code' => ResponseCode::SERVER_ERROR,
|
|
'message' => 'Internal Server Error.'
|
|
], JSON_UNESCAPED_UNICODE);
|
|
|
|
return $response->withHeader('Server', 'Lumen IM')->withStatus(500)->withBody(new SwooleStream($data));
|
|
}
|
|
|
|
/**
|
|
* @param Throwable $throwable
|
|
* @return bool
|
|
*/
|
|
public function isValid(Throwable $throwable): bool
|
|
{
|
|
return true;
|
|
}
|
|
}
|