在 Symfony2 dev env 控制器中捕获 swiftmailer 异常 [英] Catch swiftmailer exception in Symfony2 dev env controller

查看:32
本文介绍了在 Symfony2 dev env 控制器中捕获 swiftmailer 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我没有在我的控制器中捕获来自 Swiftmailer 的异常.我做错了什么或遗漏了什么?

Im not sure why Im not catching exceptions from Swiftmailer in my controller. What am I doing wrong, or missing?

在我的控制器中:

try {
    $this->get('mailer')->send($email);
}
catch (\Swift_TransportException $e) {
    $result = array(
        false, 
        'There was a problem sending email: ' . $e->getMessage()
    );
}

它似乎在到达我的代码之前被 Symfony 捕获,所以我无法自己处理错误,而是获得了标准的 500 页Swift_TransportException:无法建立连接

It seems to get caught by Symfony before it gets to my code, so instead of being able to handle the error myself I get the standard 500 page with Swift_TransportException: Connection could not be established

如果无法发送电子邮件,则无需停止申请,因为电子邮件并不重要 - 我只想发出通知.

If the email can't be sent there is no need for the application to halt as the email isn't critical - I just want to issue a notice.

也许有一种方法可以禁用 Symfonys 对某些异常或某些控制器的捕获?

Maybe there's a way to disable Symfonys catching of certain exceptions or for certain Controllers?

推荐答案

当你做 $this->container->get("mailer")->send($email); 如果您打开了假脱机,则此时不会发送电子邮件.请参阅 http://symfony.com/doc/current/cookbook/email/spool.html

When you do $this->container->get("mailer")->send($email); the email message is not being sent at that point if you have spooling turned on. See http://symfony.com/doc/current/cookbook/email/spool.html

如果你有 spool: { type: memory } 的默认设置,\Swift_TransportException 将在内核终止阶段抛出,在你的控制器退出后.解决此问题的一种方法是关闭假脱机(但是您的用户可能必须在发送电子邮件时等待),或者您可以创建自己的事件侦听器来处理异常.http://symfony.com/doc/current/cookbook/service_container/event_listener.html

If you have the default setting of spool: { type: memory }, the \Swift_TransportException will be thrown during the kernel termination phase, after your controller has exited. One way around this is to turn off the spooling (but then your users might have to wait while the email is sent), or you can make your own eventlistener to handle the exception. http://symfony.com/doc/current/cookbook/service_container/event_listener.html

这篇关于在 Symfony2 dev env 控制器中捕获 swiftmailer 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆