Commit ccbad769 authored by 王源's avatar 王源 🎧

Merge branch 'develop' into 2.0

parents d151b6bc df76c0ea
......@@ -15,12 +15,15 @@ class ConfigProvider
{
return [
'exceptions' => [
/**
* 以下异常处理器会合并到项目的config/autoload/exceptions.php文件配置数组的前面;
* 请勿在此使用顶级异常捕获处理器,防止项目中异常处理器无效;
*/
'handler' => [
'http' => [
\Meibuyu\Micro\Exceptions\Handler\MicroExceptionHandler::class,
\Meibuyu\Micro\Exceptions\Handler\QueryExceptionHandler::class,
\Meibuyu\Micro\Exceptions\Handler\PhpSpreadsheetExceptionHandler::class,
\Meibuyu\Micro\Exceptions\Handler\AppExceptionHandler::class,
],
],
],
......
......@@ -10,6 +10,7 @@ use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Logger\LoggerFactory;
use Meibuyu\Micro\Amqp\Producer\ExceptionLogProducer;
use Psr\Container\ContainerInterface;
......@@ -40,24 +41,33 @@ class AppExceptionHandler extends ExceptionHandler
*/
protected $config;
/**
* @var RequestInterface
*/
protected $request;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->stdoutLogger = $container->get(StdoutLoggerInterface::class);
$this->logger = $container->get(LoggerFactory::class)->get('Uncaught Exception');
$this->config = $container->get(ConfigInterface::class);
$this->request = $container->get(RequestInterface::class);
}
public function handle(Throwable $throwable, ResponseInterface $response)
{
// 捕获所有未捕获的异常
$this->stopPropagation();
$api = sprintf('%s(%s)', $this->request->getUri(), $this->request->getMethod());
$exceptionClass = get_class($throwable);
$message = $throwable->getMessage();
$line = $throwable->getLine();
$file = $throwable->getFile();
$trace = $throwable->getTraceAsString();
$code = $throwable->getCode();
$trace = $throwable->getTraceAsString();
$data = [
'api' => $api,
'server' => $this->config->get('app_name'),
'file' => $file,
'line' => $line,
......@@ -73,10 +83,10 @@ class AppExceptionHandler extends ExceptionHandler
} catch (Exception $e) {
put_log('异常日志失败; ' . $e->getMessage(), 'ExceptionLogProducer.log');
}
$msg = sprintf('%s[%s] in %s', $message, $line, $file);
$this->stdoutLogger->error($msg);
$this->logger->error($msg);
$this->stdoutLogger->error($trace);
$msg = sprintf('%s: %s(%s) in %s:%s', $exceptionClass, $message, $code, $file, $line);
$error = sprintf("API: %s\n%s\nStack trace:\n%s", $api, $msg, $trace);
$this->logger->error($error);
$this->stdoutLogger->error($error);
return $response->withStatus(500)->withBody(new SwooleStream($msg));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment