Skip to content

Commit

Permalink
attribute to deny translating api errors to exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejhlavacek committed Dec 12, 2012
1 parent 44f8153 commit cd9f261
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Keboola/StorageApi/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class Client
const PARTIAL_UPDATE = true;
const INCREMENTAL_UPDATE = true;

// Throw an Exception if Storage API returns an error
// If false, just return the error response
public $translateApiErrors = true;

// Token string
public $token;

Expand All @@ -28,6 +32,7 @@ class Client
// Log anonymous function
private static $_log;


/**
* @param $tokenString
* @param $url API Url
Expand Down Expand Up @@ -761,14 +766,14 @@ private function _parseResponse($jsonString)
if (is_string($data)) {
return $data;
}
if(isset($data["error"])) {
if($this->translateApiErrors && isset($data["error"])) {
$stringCode = null;
if (isset($data['code'])) {
$stringCode = $data['code'];
}
throw new ClientException($data["error"], null, null, $stringCode);
}
if(isset($data["status"]) && $data["status"] == "maintenance") {
if($this->translateApiErrors && isset($data["status"]) && $data["status"] == "maintenance") {
throw new ClientException($data["reason"], null, null, "MAINTENANCE", $data);
}
if (count($data) === 1 && isset($data["uri"])) {
Expand Down
29 changes: 29 additions & 0 deletions tests/Keboola/StorageApi/ExceptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
*
* Test if an error message from API raises a ClientException
*
* User: Ondrej Hlavacek
* Date: 11.12.12
* Time: 17:22 PST
*
*/

class Keboola_StorageApi_ExceptionsTest extends StorageApiTestCase
{
/**
* @expectedException Keboola\StorageApi\ClientException
*/
public function testException()
{
$this->_client->getTable("nonexistingtable");
}

public function testErrorMessage()
{
$this->_client->translateApiErrors = false;
$response = $this->_client->getTable("nonexistingtable");
$this->assertEquals("accessDenied", $response["code"]);
$this->_client->translateApiErrors = true;
}
}

0 comments on commit cd9f261

Please sign in to comment.