【发布时间】:2016-08-03 04:38:51
【问题描述】:
我正在处理异常并尝试创建一个异常侦听器,其中将记录与数据库相关的异常并通过电子邮件发送。
但问题是当我捕获抛出的异常以向用户显示消息时未调用侦听器:
try {
try {
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('AppBundle:User')
->getUserByEmail('abc@example.com');
}
catch(\Doctrine\DBAL\DBALException $e) {
throw new \Doctrine\DBAL\DBALException('DBAL error!!');
}
}
catch(\Exception $e) {
echo $e->getMessage();
}
错误未呈现时调用相同的侦听器:
try {
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('AppBundle:User')
->getUserByEmail('abc@example.com');
}
catch(\Doctrine\DBAL\DBALException $e) {
throw new \Doctrine\DBAL\DBALException('DBAL error!!');
}
异常监听器:
class ExceptionListener {
public function onKernelException(GetResponseForExceptionEvent $event) {
$exception = $event->getException();
if ($exception instance of \Doctrine\DBAL\DBALException) {
//log the error
}
...
...
}
}
我想做的是在一个地方正确管理错误。谢谢。
【问题讨论】:
-
顺便说一句,你已经看到Configure Monolog to Email Errors了吗?
-
是的并且可能会实现它。谢谢。
标签: php exception-handling symfony