Files
hyperf_service/app/Exception/Handler/AppExceptionHandler.php
2024-11-12 16:34:06 +08:00

49 lines
1.6 KiB
PHP

<?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;
use App\Lib\AdminReturn;
use App\Lib\Log;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\ResponseInterface;
use Throwable;
class AppExceptionHandler extends ExceptionHandler
{
public function __construct(protected StdoutLoggerInterface $logger,protected Log $log,protected AdminReturn $return)
{
}
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->log->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
$this->log->error($throwable->getTraceAsString());
// $result = $this->return->error('Internal Server Error.');
// return $response->withHeader("Content-Type", "application/json")
// ->withStatus(200)
// ->withBody(new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE)));
return $response->withHeader('Server', 'Hyperf')->withStatus(500)->withBody(new SwooleStream('Internal Server Error.'));
}
public function isValid(Throwable $throwable): bool
{
return true;
}
}