mercredi 25 novembre 2015

Catch FatalError Exception using SoapWrapper in PhP

I'm using Laravel SoapClient Wrapper for Soap calls in my application. I use it as model extension so I can use it easily in each controller which needs soap calls. So far is all excellent, but i wanna be sure that my app don't crash if WSDL URL is incorrect or service call doesn't exist on SOAP server etc... I tried to "try-catch" those parts of code but i wasn't successful. My code is next :

use Artisaninweb\SoapWrapper\Extension\SoapService;
use Artisaninweb\SoapWrapper\Facades\SoapWrapper;
use Psy\Exception\ErrorException;

class Wsdl extends SoapService{
  protected $name = 'test';
  protected $wsdl;
  protected $trace = true;

  public function __construct($wsdl)
  {
    $this->wsdl = $wsdl;
    SoapWrapper::add(function ($service) {
        $service
            ->name($this->name)
            ->wsdl($this->wsdl)
            ->trace(true);
    });
  }

  public function functions()
  {
    return $this->getFunctions();
  }

  public function bills(Array $data)
  {   //function for get bills for one customer
    $bill= null;
    try{
        SoapWrapper::service($this->name, function ($service) use ($data, &$bill) {
            $bill= $service->call('getBill', [$data])->billStructure;
        });
        return $bill;
    }
    catch(ErrorException $e){
        dd("problem");
    }
  }   

}      

If i have non existing structure of XML (wrong name of xml for response) in

$bill= $service->call('getBill', [$data])->billStructure then i get

ErrorException in wsdl.php line 37:
Undefined property: stdClass::$billStructure

If i have wrong name of service in $bill= $service->call('getBill', [$data])->billStructure then i get

SoapFault in Service.php line 218:
Function ("getBill") is not a valid method for this service

If my WSDL link is wrong (incorrect) then i get

SoapFault in Service.php line 356:
SOAP-ERROR: Parsing WSDL: Couldn't load from 'link' : failed to load external entity "link"

So my question is how to handle those Exceptions so i can provide a message to a user of my app (There was a problem with getting data, try to contact administrator...)?

In my code you can see that i have tried some kind of Try-Catch but didn't worked, so any advice would be nice.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire