Skip to content
This repository has been archived by the owner on Apr 18, 2019. It is now read-only.

Error Handling

BenTheDesigner edited this page Feb 12, 2013 · 3 revisions

This library uses exceptions to handle errors reported by the API. The type of exception thrown will depend on the API method called (details of which methods return which errors can be found here), and relate directly to the HTTP status returned by the API server. If the status code is not detailed in the Dropbox API documentation (XXX), a Dropbox\Exception will be thrown. At present this library provides the following:

HTTP Status Code Exception Type
304 \Dropbox\Exception\NotModifiedException
400 \Dropbox\Exception\BadRequestException
404 \Dropbox\Exception\NotFoundException
406 \Dropbox\Exception\NotAcceptableException
415 \Dropbox\Exception\UnsupportedMediaTypeException
XXX \Dropbox\Exception

API calls should be wrapped in try/catch blocks where you can handle them appropriately. This example attempts to retrieve metadata for a path. If the contents has not changed since the hash was returned then a NotFoundException will be thrown and we can return the cached data instead:

// Instantiate our imaginary cache object
$cache = new \Acme\Cache;

// Set the path to retrieve metadata for
$path = '/';

// Generate a cache key for this path
$cacheKey = 'metadata_' . md5($path);

try {
    // Attempt to retrieve the data from cache
    $cached = $cache->get($cacheKey);
    $hash = (isset($cached['hash'])) ? $cached['hash'] : null;

    // If the contents has not been modified since $hash was returned,
    // a NotModifiedException will be thrown when this call is made
    $metaData = $dropbox->metaData('/', null, 1000, $hash);
    
    // If no Exception was thrown, cache the data returned from the API along with the hash
    $data = array('data' => $metaData['body']->contents, 'hash' => $metaData['body']->hash);
    $cache->store($cacheKey, $data, 604800); // Cache for one week

    // Return the data we just retrieved
    return $data['data'];
} catch (\Dropbox\Exception\NotModifiedException $e) {
    // Return the cached data
    return $cached['data'];
}
Clone this wiki locally