如何抑制 symfony Controller /swift 消息中的错误消息,例如如果用户的电子邮件不存在,则 smpt 服务器有问题或电子邮件地址根本不符合 RFC 2822。
例如,我们遇到以下严重错误...
request.CRITICAL: Swift_RfcComplianceException: Address in mailbox given [ xxx@xxx.com ] does not comply with RFC 2822, 3.6.2. (uncaught exception) at $
...然后用户会得到一个 symfony 错误页面“发生错误”,无论如何我都需要抑制它。
不幸的是,一个简单的@$this->get('mailer')->send($message);在这里不起作用......
protected function generateEmail($name)
{
$user = $this->getDoctrine()
->getRepository('XXX')
->findOneBy(array('name' => $name));
if (!$user) {
exit();
}
else {
$message = \Swift_Message::newInstance()
->setSubject('xxx')
->setFrom(array('xxx@xxx.com' => 'xxx'))
->setTo($user->getEmail())
->setContentType('text/html')
->setBody(
$this->renderView(
'AcmeBundle:Template:mail/confirmed.html.twig'
)
)
;
$this->get('mailer')->send($message);
// a simple @$this->get('mailer')->send($message); doesn't work here
}
return true;
}
最佳答案
要简单地抑制错误,请将发送方法包装在 try-catch 中堵塞。明智地选择异常类型。以下示例仅捕获 Swift_RfcComplicanceExceptions
.
try {
$this->get('mailer')->send($message);
} catch (\Swift_RfcComplianceException $exception) {
// do something like proper error handling or log this error somewhere
}
我认为事先应用一些验证是明智的,这样您就可以向用户显示一个很好的错误。
关于symfony - 在 Symfony/Twig 中抑制错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25972410/