Php symfony exception handling / error handling

Working with a Symfony application using nusoap (is this the best method for integrating soap work with php / symfony?) To accept credit card payments.

I have simplified the example of my code below.

What I'm fighting is the best way to handle exceptions. The example below shows only 1 user exception (where should my user exceptions be in the symfony? Directory structure (Lib / exception?)). But what happens when there are several different types of exceptions that handle a specific error? It's not very elegant to have a try / catch block with 20 odd exceptions.

I am also not sure where I should rush and catch. I need to install some custom flashes in order to warn the user about any problems, so I believe that the trick should be done in the action controller, and not in the class that handles the soap call.

Can anyone advise where I can go wrong?

I hate dirty code / solutions and want to stick to the DRY principle as much as possible. I think I could also skip some of the symfony built-in functions that could help with this, but whenever I look, I usually find examples that are for symfony 1.2, I use 1.4.

Some examples would be great, thanks.

Library / 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();
    }
  }
}

Applications / interface / payment / action / 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 */
   }
}
+3
1

Symfony , , . , - :

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
  }
}

SOAP, , .., , , . SoapException, SoapExceptions, . .

, , lib/exception.

+3
source

Source: https://habr.com/ru/post/1761370/


All Articles