Manuel Pear

Error Handling

Error Handling -- Error Handling in Services_Ebay

Exceptions in Services_Ebay

As Services_Ebay is a PHP 5 only package, it uses exception handling and the PEAR_Exception class as base class for all exceptions. Exceptions can be thrown, whenever you try to call any of the API calls provided by Services_Ebay, which means you should always nest those in a try/catch-block:

Exemple 67-1. Exception handling

  1. <?php
  2. require_once 'Services/Ebay.php'; 
  3.    
  4. // pass some authentication data
  5. $session = Services_Ebay::getSession($devId, $appId, $certId);  
  6. $session->setToken($token);  
  7.  
  8. // create new proxy object
  9. $ebay = new Services_Ebay($session);  
  10. try { 
  11.    // call a method
  12.    echo $ebay->GeteBayOfficialTime();  
  13. } catch (Exception $e) { 
  14.    echo "Something went wrong."; 
  15.    echo $e;  
  16. ?> 

When calling a non-existent API call or passing the wrong parameters to the API, eBay will abort the API call and return an XML-document that contains error information. Services_Ebay will automatically convert this into an exception that can be easily handled by your PHP application.

Warnings in Services_Ebay

In some cases, the eBay API will still process your request, even if you passed invalid parameters and include error information in the resulting XML-document alongside the actual response of your request.

In this case, the errors will be tagged as warnings, as they were not serious errors. Services_Ebay will not convert these errors to exceptions, but only to instances of Services_Ebay_Error. These objects will be stored in the Services_Ebay_Session and can be retrieved by your application at a later point.

Exemple 67-2. Handling warnings

  1. <?php
  2. require_once 'Services/Ebay.php'; 
  3.    
  4. // pass some authentication data
  5. $session = Services_Ebay::getSession($devId, $appId, $certId);  
  6. $session->setToken($token);  
  7.  
  8. // create new proxy object
  9. $ebay = new Services_Ebay($session);  
  10. try { 
  11.    // call a method
  12.    echo $ebay->GeteBayOfficialTime();  
  13. } catch (Exception $e) { 
  14.    // Just ignore the exception and handle them
  15.    // with any warnings, that might have occured.
  16. }  
  17.  
  18. $errors = $session->getErrors();  
  19. if (count($errors) == 0) { 
  20.    echo "No errors or warnings.\n";  
  21. } else { 
  22.    foreach ($errors as $error) { 
  23.       printf("%s: %s (%d))\n", $error->getSeverity(), $error->getLongMessage(), $error->getCode()); 
  24.    }  
  25. ?> 

Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Bretagne - php - Moto - Kit graphique