Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Revised HTTP exceptions

  • Loading branch information...
commit c65981b869607afa7d720b550a6c15d4600a4b9b 1 parent 425fbcd
@its-clee its-clee authored
View
18 lib/ObjectStorage.php
@@ -158,7 +158,7 @@ protected function authenticate()
if ($this->isAcceptableResponse($response->getStatusCode())) {
return $response;
} else {
- throw new ObjectStorage_Exception_Http(null, $response->getStatusCode());
+ throw ObjectStorage_Exception_Http::factory(null, $response->getStatusCode());
}
}
@@ -299,7 +299,7 @@ public function get(ObjectStorage_Abstract $objectStorageObject, $retrieveBody =
$objectStorageObject->setResponse($response);
return $objectStorageObject;
} else {
- throw new ObjectStorage_Exception_Http('Failed to retrieve "' . $objectStorageObject->getPath() . '".', $response->getStatusCode());
+ throw ObjectStorage_Exception_Http::factory('Failed to retrieve "' . $objectStorageObject->getPath() . '".', $response->getStatusCode());
}
}
@@ -324,13 +324,13 @@ public function getClusterInfo()
try {
$response = $client->request();
} catch (Exception $e) {
- throw new ObjectStorage_Exception_Http('Failed to retrieve users.');
+ throw ObjectStorage_Exception_Http::factory('Failed to retrieve users.');
}
if ($this->isAcceptableResponse($response->getStatusCode())) {
return $response->getBody();
} else {
- throw new ObjectStorage_Exception_Http('Failed to retrieve users.', $response->getStatusCode());
+ throw ObjectStorage_Exception_Http::factory('Failed to retrieve users.', $response->getStatusCode());
}
}
@@ -375,14 +375,14 @@ public function getCdnUrls(ObjectStorage_Abstract $objectStorageObject)
if ($this->isAcceptableResponse($response->getStatusCode())) {
$objectStorageObject->setResponse($response);
} else {
- throw new ObjectStorage_Exception_Http('Failed to retrieve "' . $objectStorageObject->getPath() . '".', $response->getStatusCode());
+ throw ObjectStorage_Exception_Http::factory('Failed to retrieve "' . $objectStorageObject->getPath() . '".', $response->getStatusCode());
}
}
$path = '/' . ltrim($objectStorageObject->getPath(), '/');
foreach ($objectStorageObject->getResponse()->getHeaders() as $key => $val) {
if (in_array(strtoupper($key), array('X-CDN-URL', 'X-CDN-STREAM-HTTP-URL', 'X-CDN-STREAM-FLASH-URL',
- 'X-CDN-CUSTOM-URL', 'X-CDN-CUSTOM-STREAM-HTTP-URL', 'X-CDN-CUSTOM-STREAM-FLASH-URL'))) {
+ 'X-CDN-CUSTOM-URL', 'X-CDN-CUSTOM-STREAM-HTTP-URL', 'X-CDN-CUSTOM-STREAM-FLASH-URL'))) {
$cdnUrls[] = $val;
}
}
@@ -452,7 +452,7 @@ public function create(ObjectStorage_Abstract $objectStorageObject)
$objectStorageObject->setResponse($response);
return $objectStorageObject;
} else {
- throw new ObjectStorage_Exception_Http('Failed to create ' . $objectStorageObject . '.', $response->getStatusCode());
+ throw ObjectStorage_Exception_Http::factory('Failed to create ' . $objectStorageObject . '.', $response->getStatusCode());
}
}
@@ -491,7 +491,7 @@ public function update(ObjectStorage_Abstract $objectStorageObject)
$objectStorageObject->setResponse($response);
return $objectStorageObject;
} else {
- throw new ObjectStorage_Exception_Http('Failed to save ' . $objectStorageObject . '. ' . $response->getBody(), $response->getStatusCode());
+ throw ObjectStorage_Exception_Http::factory('Failed to save ' . $objectStorageObject . '. ' . $response->getBody(), $response->getStatusCode());
}
}
@@ -528,7 +528,7 @@ public function delete(ObjectStorage_Abstract $objectStorageObject)
$objectStorageObject->setResponse($response);
return true;
} else {
- throw new ObjectStorage_Exception_Http(null, $response->getStatusCode());
+ throw ObjectStorage_Exception_Http::factory(null, $response->getStatusCode());
}
}
View
2  lib/ObjectStorage/Abstract.php
@@ -276,7 +276,7 @@ public function getBody()
{
return $this->response->getBody();
}
-
+
/**
* Sets a location to copy from for an object
*
View
2  lib/ObjectStorage/Container.php
@@ -57,7 +57,7 @@ public function addCname($cnameUrl, $type='HTTP')
return $this;
}
-
+
/**
* Removes CNAME. This method removes the CNAME from the container.
* You have to call ObjectStorage_Abstract::create or ObjectStorage_Abstract_update method in order for this to take affect.
View
21 lib/ObjectStorage/Exception/Http.php 100644 → 100755
@@ -1,19 +1,13 @@
<?php
-class ObjectStorage_Exception_Http extends ObjectStorage_Exception
+class ObjectStorage_Exception_Http extends ObjectStorage_Exception implements ObjectStorage_Exception_Http_Interface
{
public function __construct($message = null, $code = 0)
{
parent::__construct($message, $code);
-
- if (is_numeric($code) && $code > 0) {
- $this->throwException($code, $message, $this->file, $this->line);
- } else {
- throw new ObjectStorage_Exception($message);
- }
}
- protected function throwException($responseCode, $message, $class, $line)
+ public static function factory($message = null, $responseCode = 0)
{
$errorMessage = null;
switch ($responseCode) {
@@ -71,10 +65,13 @@ protected function throwException($responseCode, $message, $class, $line)
$exception = 'ObjectStorage_Exception';
}
- $newException = new $exception($message);
- $newException->setFile($class);
- $newException->setLine($line);
+ $newException = new $exception($message, $responseCode);
+
+ // Set the initial caller's file name and line number instead of this factory method
+ $stackTrace = debug_backtrace();
+ $newException->setFile($stackTrace[0]['file']);
+ $newException->setLine($stackTrace[0]['line']);
- throw $newException;
+ return $newException;
}
}
View
2  lib/ObjectStorage/Exception/Http/BadGateway.php 100644 → 100755
@@ -1,4 +1,4 @@
<?php
-class ObjectStorage_Exception_Http_BadGateway extends ObjectStorage_Exception
+class ObjectStorage_Exception_Http_BadGateway extends ObjectStorage_Exception implements ObjectStorage_Exception_Http_Interface
{}
View
2  lib/ObjectStorage/Exception/Http/BadRequest.php 100644 → 100755
@@ -1,4 +1,4 @@
<?php
-class ObjectStorage_Exception_Http_BadRequest extends ObjectStorage_Exception
+class ObjectStorage_Exception_Http_BadRequest extends ObjectStorage_Exception implements ObjectStorage_Exception_Http_Interface
{}
View
2  lib/ObjectStorage/Exception/Http/Conflict.php 100644 → 100755
@@ -1,4 +1,4 @@
<?php
-class ObjectStorage_Exception_Http_Conflict extends ObjectStorage_Exception
+class ObjectStorage_Exception_Http_Conflict extends ObjectStorage_Exception implements ObjectStorage_Exception_Http_Interface
{}
View
2  lib/ObjectStorage/Exception/Http/Forbidden.php 100644 → 100755
@@ -1,4 +1,4 @@
<?php
-class ObjectStorage_Exception_Http_Forbidden extends ObjectStorage_Exception
+class ObjectStorage_Exception_Http_Forbidden extends ObjectStorage_Exception implements ObjectStorage_Exception_Http_Interface
{}
View
6 lib/ObjectStorage/Exception/Http/Interface.php
@@ -0,0 +1,6 @@
+<?php
+/**
+ * HTTP Exception mark interface
+ */
+Interface ObjectStorage_Exception_Http_Interface
+{}
View
2  lib/ObjectStorage/Exception/Http/InternalServerError.php 100644 → 100755
@@ -1,4 +1,4 @@
<?php
-class ObjectStorage_Exception_Http_InternalServerError extends ObjectStorage_Exception
+class ObjectStorage_Exception_Http_InternalServerError extends ObjectStorage_Exception implements ObjectStorage_Exception_Http_Interface
{}
View
2  lib/ObjectStorage/Exception/Http/MethodNotAllowed.php 100644 → 100755
@@ -1,4 +1,4 @@
<?php
-class ObjectStorage_Exception_Http_MethodNotAllowed extends ObjectStorage_Exception
+class ObjectStorage_Exception_Http_MethodNotAllowed extends ObjectStorage_Exception implements ObjectStorage_Exception_Http_Interface
{}
View
2  lib/ObjectStorage/Exception/Http/NotAcceptable.php 100644 → 100755
@@ -1,4 +1,4 @@
<?php
-class ObjectStorage_Exception_Http_NotAcceptable extends ObjectStorage_Exception
+class ObjectStorage_Exception_Http_NotAcceptable extends ObjectStorage_Exception implements ObjectStorage_Exception_Http_Interface
{}
View
2  lib/ObjectStorage/Exception/Http/NotFound.php 100644 → 100755
@@ -1,4 +1,4 @@
<?php
-class ObjectStorage_Exception_Http_NotFound extends ObjectStorage_Exception
+class ObjectStorage_Exception_Http_NotFound extends ObjectStorage_Exception implements ObjectStorage_Exception_Http_Interface
{}
View
2  lib/ObjectStorage/Exception/Http/NotImplemented.php 100644 → 100755
@@ -1,4 +1,4 @@
<?php
-class ObjectStorage_Exception_Http_NotImplemented extends ObjectStorage_Exception
+class ObjectStorage_Exception_Http_NotImplemented extends ObjectStorage_Exception implements ObjectStorage_Exception_Http_Interface
{}
View
2  lib/ObjectStorage/Exception/Http/ProxyAuthenticationRequired.php 100644 → 100755
@@ -1,4 +1,4 @@
<?php
-class ObjectStorage_Exception_Http_ProxyAuthenticationRequired extends ObjectStorage_Exception
+class ObjectStorage_Exception_Http_ProxyAuthenticationRequired extends ObjectStorage_Exception implements ObjectStorage_Exception_Http_Interface
{}
View
2  lib/ObjectStorage/Exception/Http/RequestTimeout.php 100644 → 100755
@@ -1,4 +1,4 @@
<?php
-class ObjectStorage_Exception_Http_RequestTimeout extends ObjectStorage_Exception
+class ObjectStorage_Exception_Http_RequestTimeout extends ObjectStorage_Exception implements ObjectStorage_Exception_Http_Interface
{}
View
2  lib/ObjectStorage/Exception/Http/Unauthorized.php 100644 → 100755
@@ -1,4 +1,4 @@
<?php
-class ObjectStorage_Exception_Http_Unauthorized extends ObjectStorage_Exception
+class ObjectStorage_Exception_Http_Unauthorized extends ObjectStorage_Exception implements ObjectStorage_Exception_Http_Interface
{}
View
2  lib/ObjectStorage/Object.php
@@ -129,7 +129,7 @@ public function deleteAt($epochTimestamp)
$this->request->setHeader('X-Delete-At', $epochTimestamp);
return $this;
}
-
+
/**
* Sets the number of seconds to wait before deleting an object
* (This is converted into an 'X-Delete-At' header in the container
View
42 tests/AuthenticationTest.php 100644 → 100755
@@ -3,6 +3,16 @@
class AuthenticationTest extends BaseTest
{
+ /**
+ * testAuthenticationWithBadCredentials test sets a dummy token store.
+ * We need to set the proper token store object for other test(s).
+ */
+ public function setUp()
+ {
+ self::$tokenStore = ObjectStorage_TokenStore::factory('file', array('ttl' => 3600, 'path' => '/tmp'));
+ ObjectStorage::setTokenStore(self::$tokenStore);
+ }
+
public function testAuthentication()
{
// Removes cached authentication data if any
@@ -13,4 +23,36 @@ public function testAuthentication()
$this->assertObjectHasAttribute('objectStorageUrl', $authData);
$this->assertObjectHasAttribute('authToken', $authData);
}
+
+ /**
+ * @expectedException ObjectStorage_Exception_Http_Unauthorized
+ */
+ public function testAuthenticationWithBadCredentials()
+ {
+ $options = array('adapter' => ObjectStorage_Http_Client::SOCKET, 'timeout' => 10);
+ $objectStorage = new ObjectStorage(self::$host, self::$username, 'bad-password-' . time(), $options);
+
+ // Make sure we are not using cached auth data
+ ObjectStorage::setTokenStore(new AuthenticationTest_TokenStore());
+
+ $authData = $objectStorage->getAuthenticationData();
+ }
+}
+
+class AuthenticationTest_TokenStore implements ObjectStorage_TokenStore_Interface
+{
+ public function get($key)
+ {
+ return null;
+ }
+
+ public function set($key, $data)
+ {
+ return true;
+ }
+
+ public function delete($key)
+ {
+ return true;
+ }
}
View
41 tests/ExceptionTest.php
@@ -0,0 +1,41 @@
+<?php
+require_once(dirname(__FILE__) . '/BaseTest.php');
+
+class ExceptionTest extends BaseTest
+{
+ public function testHttpExceptions()
+ {
+ $httpCodes = array(
+ 400 => 'BadRequest',
+ 401 => 'Unauthorized',
+ 403 => 'Forbidden',
+ 404 => 'NotFound',
+ 405 => 'MethodNotAllowed',
+ 406 => 'NotAcceptable',
+ 407 => 'ProxyAuthenticationRequired',
+ 408 => 'RequestTimeout',
+ 409 => 'Conflict',
+ 500 => 'InternalServerError',
+ 501 => 'NotImplemented',
+ 502 => 'BadGateway'
+ );
+
+ foreach ($httpCodes as $httpCode => $classSuffix) {
+
+ $e = ObjectStorage_Exception_Http::factory('', $httpCode);
+
+ $this->assertInstanceOf('ObjectStorage_Exception_Http_' . $classSuffix, $e);
+ $this->assertEquals($httpCode, $e->getCode());
+ }
+ }
+
+ public function testHttpExceptionInvalidCode()
+ {
+ $invalidCode = 1000000000;
+ $e = ObjectStorage_Exception_Http::factory('', $invalidCode);
+
+ // Expecting the base HTTP exception
+ $this->assertInstanceOf('ObjectStorage_Exception_Http', $e);
+ $this->assertEquals($invalidCode, $e->getCode());
+ }
+}
View
8 tests/ObjectTest.php
@@ -103,4 +103,12 @@ public function testDelete()
@unlink(self::$localFileName);
}
+
+ /**
+ * @expectedException ObjectStorage_Exception_Http_NotFound
+ */
+ public function testDeleteNonExistingObject()
+ {
+ $result = self::$objectStorage->with('i-do-not-exist-' . time())->delete();
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.