PEAR Manual
Prev |  | Next

# 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:

Example 60-1. Exception handling
    require_once 'Services/Ebay.php';

    // pass some authentication data
    $session = Services_Ebay::getSession($devId, $appId, $certId);
    $session->setToken($token);

    // create new proxy object
    $ebay = new Services_Ebay($session);
    try {
        // call a method
        echo $ebay->GeteBayOfficialTime();
    } catch (Exception $e) {
        echo "Something went wrong.";
        echo $e;
    }

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.

Example 60-2. Handling warnings
    require_once 'Services/Ebay.php';

    // pass some authentication data
    $session = Services_Ebay::getSession($devId, $appId, $certId);
    $session->setToken($token);

    // create new proxy object
    $ebay = new Services_Ebay($session);
    try {
        // call a method
        echo $ebay->GeteBayOfficialTime();
    } catch (Exception $e) {
        // Just ignore the exception and handle them
        // with any warnings, that might have occured.
    }

    $errors = $session->getErrors();
    if (count($errors) == 0) {
        echo "No errors or warnings.\n";
    } else {
        foreach ($errors as $error) {
            printf("%s: %s (%d))\n", $error->getSeverity(), $error->getLongMessage(), $error->getCode());
        }
    }

Prev | Home | Next
XML encoding | Up | Architecture