Commit 4126a96d authored by 王源's avatar 王源
Browse files

Merge branch 'feature/exception'

parents 2502dd11 3c948737
Loading
Loading
Loading
Loading
+2 −1
Original line number Original line Diff line number Diff line
@@ -27,7 +27,8 @@
    "hyperf/service-governance": "~1.1.0",
    "hyperf/service-governance": "~1.1.0",
    "fzaninotto/faker": "^1.9",
    "fzaninotto/faker": "^1.9",
    "phpoffice/phpspreadsheet": "^1.8",
    "phpoffice/phpspreadsheet": "^1.8",
    "dimsav/unix-zipper": "1.*"
    "dimsav/unix-zipper": "1.*",
    "hyperf/amqp": "~1.1.0"
  },
  },
  "autoload": {
  "autoload": {
    "psr-4": {
    "psr-4": {
+47 −0
Original line number Original line Diff line number Diff line
<?php

declare(strict_types=1);

namespace Meibuyu\Micro\Amqp\Producer;

use Exception;
use Hyperf\Amqp\Annotation\Producer;
use Hyperf\Amqp\Message\ProducerMessage;
use Meibuyu\Micro\Exceptions\HttpResponseException;
use Meibuyu\Micro\Model\Auth;

/**
 * @Producer(exchange="micro", routingKey="exception-log")
 */
class ExceptionLogProducer extends ProducerMessage
{

    public function __construct($data)
    {
        try {
            $data['operator'] = Auth::user()['name'];
        } catch (HttpResponseException $e) {
            put_log('获取操作人失败; ' . $e->getMessage(), 'ExceptionLogProducer.log');
        }
        // 排除依赖包文件和运行缓存文件
        put_log("file: {$data['file']}", 'ExceptionLogProducerEcho.log');
        if (strpos($data['file'], '/vendor/') === false && strpos($data['file'], '/runtime/') === false) {
            try {
                put_log("git: git blame -L {$data['line']},{$data['line']} {$data['file']}", 'ExceptionLogProducerEcho.log');
                exec("git blame -L {$data['line']},{$data['line']} {$data['file']}", $output);
                put_log("output: $output", 'ExceptionLogProducerEcho.log');
                if (!empty($output[0]) && is_string($output[0])) {
                    preg_match('/(?<=\()[^ ]+/', $output[0], $matches);
                    put_log("matches: $matches", 'ExceptionLogProducerEcho.log');
                    if (!empty($matches[0])) {
                        $data['coder'] = $matches[0];
                    }
                }
            } catch (Exception $e) {
                put_log('获取编码人失败; ' . $e->getMessage(), 'ExceptionLogProducer.log');
            }
        }
        $this->payload = $data;
    }

}
+44 −5
Original line number Original line Diff line number Diff line
@@ -4,16 +4,27 @@ declare(strict_types=1);


namespace Meibuyu\Micro\Exceptions\Handler;
namespace Meibuyu\Micro\Exceptions\Handler;


use Exception;
use Hyperf\Amqp\Producer;
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\Logger\LoggerFactory;
use Hyperf\Logger\LoggerFactory;
use Meibuyu\Micro\Amqp\Producer\ExceptionLogProducer;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerInterface;
use Throwable;
use Throwable;


class AppExceptionHandler extends ExceptionHandler
class AppExceptionHandler extends ExceptionHandler
{
{

    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
    /**
     * @var StdoutLoggerInterface
     * @var StdoutLoggerInterface
     */
     */
@@ -24,20 +35,48 @@ class AppExceptionHandler extends ExceptionHandler
     */
     */
    protected $logger;
    protected $logger;


    public function __construct(StdoutLoggerInterface $stdoutLogger, LoggerFactory $loggerFactory)
    /**
     * @var ConfigInterface
     */
    protected $config;

    public function __construct(ContainerInterface $container)
    {
    {
        $this->stdoutLogger = $stdoutLogger;
        $this->container = $container;
        $this->logger = $loggerFactory->get('Uncaught Exception');
        $this->stdoutLogger = $container->get(StdoutLoggerInterface::class);
        $this->logger = $container->get(LoggerFactory::class)->get('Uncaught Exception');
        $this->config = $container->get(ConfigInterface::class);
    }
    }


    public function handle(Throwable $throwable, ResponseInterface $response)
    public function handle(Throwable $throwable, ResponseInterface $response)
    {
    {
        // 捕获所有未捕获的异常
        // 捕获所有未捕获的异常
        $this->stopPropagation();
        $this->stopPropagation();
        $msg = sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile());
        $message = $throwable->getMessage();
        $line = $throwable->getLine();
        $file = $throwable->getFile();
        $trace = $throwable->getTraceAsString();
        $code = $throwable->getCode();
        $data = [
            'server' => $this->config->get('app_name'),
            'file' => $file,
            'line' => $line,
            'message' => $message,
            'trace' => $trace,
            'code' => $code,
            'created_at' => now(),
        ];
        try {
            $exceptionLogProducer = new ExceptionLogProducer($data);
            $producer = $this->container->get(Producer::class);
            $producer->produce($exceptionLogProducer);
        } catch (Exception $e) {
            put_log('异常日志失败; ' . $e->getMessage(), 'ExceptionLogProducer.log');
        }
        $msg = sprintf('%s[%s] in %s', $message, $line, $file);
        $this->stdoutLogger->error($msg);
        $this->stdoutLogger->error($msg);
        $this->logger->error($msg);
        $this->logger->error($msg);
        $this->stdoutLogger->error($throwable->getTraceAsString());
        $this->stdoutLogger->error($trace);
        return $response->withStatus(500)->withBody(new SwooleStream($msg));
        return $response->withStatus(500)->withBody(new SwooleStream($msg));
    }
    }


+15 −0
Original line number Original line Diff line number Diff line
<?php

namespace Meibuyu\Micro\Exceptions;

use Throwable;

class RpcException extends \Exception
{

    public function __construct($message = "", $code = 511, Throwable $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }

}
+31 −11
Original line number Original line Diff line number Diff line
@@ -8,7 +8,8 @@


namespace Meibuyu\Micro\Service\Interfaces\Product;
namespace Meibuyu\Micro\Service\Interfaces\Product;


use Meibuyu\Micro\Exceptions\HttpResponseException;
use Exception;
use Meibuyu\Micro\Exceptions\RpcException;


interface ShopifyServiceInterface
interface ShopifyServiceInterface
{
{
@@ -18,7 +19,7 @@ interface ShopifyServiceInterface
     * @param $orderId
     * @param $orderId
     * @param $shopifySiteId
     * @param $shopifySiteId
     * @return array
     * @return array
     * @throws \Exception
     * @throws Exception
     */
     */
    public function pullOrder($orderId, $shopifySiteId): array;
    public function pullOrder($orderId, $shopifySiteId): array;


@@ -36,7 +37,7 @@ interface ShopifyServiceInterface
     * @param int $sinceId 订单id
     * @param int $sinceId 订单id
     * @param int $shopifySiteId shopify站点id
     * @param int $shopifySiteId shopify站点id
     * @return array
     * @return array
     * @throws \Exception
     * @throws Exception
     * @author Zero
     * @author Zero
     */
     */
    public function pullOrderList($sinceId, $shopifySiteId): array;
    public function pullOrderList($sinceId, $shopifySiteId): array;
@@ -47,23 +48,42 @@ interface ShopifyServiceInterface
     * @param array $params 更新的数据
     * @param array $params 更新的数据
     * @param int $shopifySiteId shopify站点id
     * @param int $shopifySiteId shopify站点id
     * @return mixed
     * @return mixed
     * @throws \Exception
     * @throws RpcException
     * @author Zero
     * @author zero
     */
     */
    public function updateOrder($orderId, $params, $shopifySiteId);
    public function updateOrder($orderId, $params, $shopifySiteId);


    /**
    /**
     * 为shopify订单创建发货记录
     * 创建shopify订单发货记录
     * @param int $orderId
     * @param array $params
     * 参数示例: https://shopify.dev/docs/admin-api/rest/reference/shipping-and-fulfillment/fulfillment#create-2020-07
     * 参数示例: https://shopify.dev/docs/admin-api/rest/reference/shipping-and-fulfillment/fulfillment#create-2020-07
     * location_id不传会默认拿取系统有的数据,拿不到报错
     * location_id不传会默认拿取系统有的数据,拿不到报错
     * @param int $orderId
     * @param array $params
     * @param int $shopifySiteId
     * @param int $shopifySiteId
     * @return mixed
     * @return mixed
     * @throws HttpResponseException
     * @throws RpcException
     * @throws \Exception
     * @author zero
     * @author Zero
     */
     */
    public function createOrderFulfillment($orderId, $params, $shopifySiteId);
    public function createOrderFulfillment($orderId, $params, $shopifySiteId);


    /**
     * 更新shopify订单发货物流信息
     * 参数示例: https://shopify.dev/docs/admin-api/rest/reference/shipping-and-fulfillment/fulfillment#update_tracking-2020-07
     * [
     *   "notify_customer" => true,
     *   "tracking_info" => [
     *     "number" => "1111",
     *     "url" => "http://www.my-url.com",
     *     "company" => "my-company",
     *    ]
     * ]
     * @param $fulfillmentId
     * @param $params
     * @param $shopifySiteId
     * @return array
     * @throws RpcException
     * @author zero
     */
    public function updateFulfillmentTracking($fulfillmentId, $params, $shopifySiteId);

}
}