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

处理异常捕获处理器配置注入

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