inakiabt / etsy-php

Etsy API wrapper for PHP
74 stars 59 forks source link

Throw exception on restricted relations #2

Closed atrauzzi closed 10 years ago

atrauzzi commented 11 years ago

Just issued a request and got the following return:

  ["error_messages"]=>
  array(1) {
    [0]=>
    string(35) "Access denied on association Images"
  }

I'm wondering if the API should be detecting this and throwing an exception?

inakiabt commented 11 years ago

Sure. Can you paste your request code here? In order to reproduce the issue it would be useful.

inakiabt commented 11 years ago

Seems that this kind of errors are because invalid associations for a given resource. If you try to get a "listing" with a "transaction" association (invalid association) it would fail.

    $args = array(
            'params' => array(
                'listing_id' => 156326352
            ),
            // A list of associations
            'associations' => array(
                // Could be a simple association, sending something like: ?includes=Images
                'Transactions',
            )
        );
$result = $api->getListing($args);
print_r($result);
 [error_messages] => Array
 (
       [0] => Access denied on association Transactions
)

But I'll add an exception if an error_messages is present on response.

inakiabt commented 11 years ago

I've just added a response exception on invalid associations errors. Please, try it and let me know ;)

FYI, EtsyResponseException has a getResponse to get the response. For example:

    $args = array(
            'params' => array(
                'listing_id' => 156314224
            ),
            // A list of associations
            'associations' => array(
                'Transactions',
            )
        );
try {
    $result = $api->getListing($args);
} catch (Etsy\EtsyResponseException $e) {
    echo "Error: " . $e->getMessage() . "\n";
    echo "Response: " . print_r($e->getResponse(), true) . "\n";
}

Output:

Error: Invalid association: Access denied on association Transactions
Response: Array
(
    [count] => 1
    [results] => Array
        (
            [0] => Array
                (
                    [listing_id] => 156314224
                    [state] => removed,
                    ...
                    [error_messages] => Array
                        (
                            [0] => Access denied on association Transactions
                        )

                )

        )

    [params] => Array
        (
            [listing_id] => 156314224
        )

    [type] => Listing
    [pagination] => Array
        (
        )

)