[英]php symfony exception handling/error handling
Working on a symfony application that uses nusoap (is this the best method for integrating soap work with php/symfony?) for taking credit card payments. 使用一个使用nusoap的symfony应用程序(这是将soap工作与php / symfony集成的最佳方法吗?)来进行信用卡付款。
I've simplified an example of my code below. 我在下面简化了我的代码示例。
What I'm struggling with is the best way to handle exceptions. 我正在努力解决的是处理异常的最佳方法。 The example below only has 1 custom exception (where should my custom exceptions reside within the directory structure of symfony? (lib/exception?)) But what happens when there are several different types of exceptions that handle a specific error?
下面的例子只有1个自定义异常(我的自定义异常应该放在symfony的目录结构中吗?(lib / exception?))但是当有几种不同类型的异常处理特定错误时会发生什么? It's not very elegant to have a try/catch block with 20 odd exceptions.
拥有20个奇数异常的try / catch块并不是很优雅。
I'm also not sure of where I should be throwing and catching. 我也不确定我应该在哪里投掷和捕捉。 I need to set some user flashes to alert the user of any problems, so I figure the catching should be done in the actions controller rather than within the class that handles the soap call.
我需要设置一些用户闪存来提醒用户任何问题,所以我认为捕获应该在操作控制器中完成,而不是在处理soap调用的类中。
Could anyone please advise where I might be going wrong? 谁能告诉我哪里可能出错?
I hate messy code/solutions and want to stick to the DRY principle as much as possible. 我讨厌凌乱的代码/解决方案,并希望尽可能坚持DRY原则。 I think I might also be missing some built in symfony functionality that might help with this but whenever I search I usually find examples that are for symfony 1.2, I'm using 1.4.
我想我可能也会遗漏一些内置的symfony功能,可能对此有所帮助,但每当我搜索时,我通常都会找到适用于symfony 1.2的示例,我使用的是1.4。
Some examples would be great, thanks. 一些例子会很棒,谢谢。
lib/soap_payment.class.php LIB / soap_payment.class.php
class SoapPayment
{
public function charge()
{
/*assume options are setup correctly for sake of example*/
try
{
$this->call();
}
catch (SoapPaymentClientFaultException $e)
{
/* should this be caught here? */
}
}
private function call()
{
$this->client->call($this->options);
if ($this->client->hasFault())
{
throw new SoapPaymentClientFaultException();
}
}
}
apps/frontend/payment/actions/actions.class.php 应用程序/前端/支付/动作/的actions.class.php
class paymentActions extends sfActions
{
public function executeCreate(sfWebRequest $request)
{
/* check form is valid etc */
$soap_payment = new SoapPayment();
try
{
$soap_payment->charge();
}
catch (SoapPaymentClientFaultException $e)
{
/* or throw/catch here? */
$this->getUser()->setFlash('error', ...);
$this->getLogger()->err(...);
}
/* save form regardless, will set a flag to check if successful or not in try/catch block */
}
}
One not very well known feature of Symfony is that exceptions can manage the content sent in a response. Symfony的一个不为人知的特性是异常可以管理响应中发送的内容。 So you could do something like this:
所以你可以这样做:
class SoapException extends sfException
{
public function printStackTrace() //called by sfFrontWebController when an sfException is thrown
{
$response = sfContext::getInstance()->getResponse();
if (null === $response)
{
$response = new sfWebResponse(sfContext::getInstance()->getEventDispatcher());
sfContext::getInstance()->setResponse($response);
}
$response->setStatusCode(5xx);
$response->setContent('oh noes'); //probably you want a whole template here that prints the message that was a part of the SoapException
}
}
If you need a cleaner handling of SOAP exceptions, like setting flashes, etc. you'll probably have to catch each exception. 如果您需要更清晰地处理SOAP异常,例如设置闪烁等,您可能必须捕获每个异常。 One idea here might be to create a generic
SoapException
class that is extended by more specific SoapExceptions so you don't have to catch a bunch of different types. 这里的一个想法可能是创建一个通用的
SoapException
类,该类由更具体的SoapExceptions扩展,因此您不必捕获一堆不同的类型。 The above code may be a useful fallback mechanism as well. 上面的代码也可能是一个有用的回退机制。
Finally, yes, you should place custom exceptions in lib/exception
. 最后,是的,您应该在
lib/exception
放置自定义lib/exception
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.