PHP code example of goletter / hyperf-exception-notify

1. Go to this page and download the library: Download goletter/hyperf-exception-notify library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

goletter / hyperf-exception-notify example snippets


return [
    'handler' => [
        'http' => [
            \Goletter\HyperfExceptionNotify\Exceptions\Handler\ExceptionNotifyHandler::class,
            \App\Exception\Handler\AppExceptionHandler::class,
            // ...
        ],
    ],
];

'dont_report' => [
    \Hyperf\HttpMessage\Exception\NotFoundHttpException::class,
    \Hyperf\HttpMessage\Exception\MethodNotAllowedHttpException::class,
    \Hyperf\Validation\ValidationException::class,
    \App\Exception\BusinessException::class,
],

'mask_fields' => [
    '*password*',
    '*token*',
    '*secret*',
    'authorization',
    'cookie',
    'set-cookie',
    'x-api-key',
],

use function Goletter\HyperfExceptionNotify\exception_notify_report;
use function Goletter\HyperfExceptionNotify\exception_notify_report_if;

try {
    $this->payService->refund($order);
} catch (\Throwable $e) {
    exception_notify_report($e);                       // 推送到配置的渠道
    exception_notify_report($e, 'dingTalk');           // 只推钉钉
    exception_notify_report($e, ['weWork', 'log']);    // 推多个渠道
}

// 也可以直接传字符串
exception_notify_report('第三方回调签名校验失败:order_id=' . $orderId);

// 条件成立才上报,条件可以是闭包
exception_notify_report_if($order->amount > 10000, $e, 'dingTalk');

use Goletter\HyperfExceptionNotify\ExceptionNotify;

public function __construct(private ExceptionNotify $notify) {}

$this->notify->report($e);
$this->notify->report($e, 'dingTalk');
$this->notify->onChannel('dingTalk')->report($e);
$this->notify->reportIf($condition, $e);

namespace App\Listener;

use Goletter\HyperfExceptionNotify\ExceptionNotify;
use Hyperf\AsyncQueue\Event\FailedHandle;
use Hyperf\Crontab\Event\FailToExecute;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;

#[Listener]
class ReportJobFailureListener implements ListenerInterface
{
    public function __construct(private ExceptionNotify $notify) {}

    public function listen(): array
    {
        return [FailedHandle::class, FailToExecute::class];
    }

    public function process(object $event): void
    {
        match (true) {
            $event instanceof FailedHandle => $this->notify->report($event->getThrowable()),
            $event instanceof FailToExecute => $this->notify->report($event->throwable),
            default => null,
        };
    }
}

Router::get('/test/exception', function () {
    throw new \RuntimeException('异常通知测试');
});

use Goletter\HyperfExceptionNotify\Collectors\Collector;

class TenantCollector extends Collector
{
    public function collect(): array
    {
        return ['tenant_id' => \Hyperf\Context\Context::get('tenant_id')];
    }
}

'sanitizers' => [
    LengthLimitSanitizer::class . ':4096',           // 截断到 4096 × 90%
    StrReplaceSanitizer::class . ':/opt/www/,',      // 去掉路径前缀
    AppendContentSanitizer::class . ':@所有人',       // 末尾追加内容
],
bash
composer rf.php vendor:publish goletter/hyperf-exception-notify