PHP-Symfony 4-Bugsnag-忽略特定的异常类型

我使用bugsnag记录我们应用的错误.该应用程序基于symfony 4构建,我有一个自定义侦听器,可捕获异常并对其进行处理.我需要告诉bugsnag忽略我手动处理的异常(因为它们已经被处理,因此无需记录它们).

我的自定义侦听器比bugsnag侦听器具有更高的优先级(因此先运行).问题在于,停止事件传播会破坏其他内容(例如,安全侦听器不再运行,因为默认情况下它的优先级低于bugsnag).

以下是我的侦听器代码(好吧……相关部分):

class ExceptionListener
{

protected $router;
private $mailerService;
private $tokenStorage;
private $request;
private $em;
/**
 * @var UtilsService
 */
private $utilsService;

public function __construct(Router $router, MailerService $mailerService, TokenStorageInterface $tokenStorage, RequestStack $request, EntityManagerInterface $em, UtilsService $utilsService)
{
    $this->router = $router;
    $this->mailerService = $mailerService;
    $this->tokenStorage = $tokenStorage;
    $this->request = $request;
    $this->em = $em;
    $this->utilsService = $utilsService;
}

public function onKernelException(ExceptionEvent $event)
{
    $exception = $event->getException();
    $message = $exception->getMessage();

    switch (true) {
        case $exception instanceof NotFoundHttpException:
             // Redirect somewhere
        break;
        case $exception instanceof CustomException:
             // Do some stuff
             $event->stopPropagation(); // This does what I need (stops propagation to bugsnag listener) but breaks other things so is not a solution (since it stops propagation to everything).
        break;
    }

    return false;
}
}

我需要的很简单…万一抛出的异常是CustomException的实例,我希望它不会被发送到bugsnag.

我看到的2种可能的解决方案是(欢迎使用c):

>告诉bugsnag以某种方式忽略该异常:在bugsnag文档中,我找到了针对Laravel(https://docs.bugsnag.com/platforms/php/laravel/configuration-options/-使用dontReport)和Ruby(https://docs.bugsnag.com/platforms/ruby/other/configuration-options/#ignore_classes)进行此操作的方法,但对于Symfony却没有.任何想法如何做到这一点?
>仅针对bugsnag侦听器停止事件的传播:无论如何,我没有发现任何文档.任何想法如何做到这一点?

解决方法:

您可以按照以下说明实现回调并检查报表对象:
https://docs.bugsnag.com/platforms/php/symfony/configuration-options/#callbacks

只需从回调中返回false即可阻止向Bugsnag报告.

上一篇:off(events,[selector],[fn]) 在选择元素上移除一个或多个事件的事件处理函数。


下一篇:【更正】“给自定义控件(Web Control)添加事件的几种方法”有一个不太准确的地方。