Added new exceptions for HTTP error codes.

This commit is contained in:
Matt Butcher
2012-01-05 09:59:47 -06:00
parent a31bf59b12
commit 16edc5372d
7 changed files with 96 additions and 6 deletions

View File

@@ -216,6 +216,11 @@ class Container implements \Countable {
$response = $client->doRequest($url, 'PUT', $headers, $obj->content()); $response = $client->doRequest($url, 'PUT', $headers, $obj->content());
if ($response->status() != 201) {
throw new \HPCloud\Exception('An unknown error occurred while saving: ' . $response->status());
}
return TRUE;
} }
/** /**

View File

@@ -69,9 +69,22 @@ class Object {
* *
* @param string $name * @param string $name
* A name (may be pathlike) for the object. * A name (may be pathlike) for the object.
* @param string $content
* Optional content to store in this object. This is the same
* as calling setContent().
* @param string $type
* Optional content type for this content. This is the same as
* calling setContentType().
*/ */
public function __construct($name) { public function __construct($name, $content = NULL, $type = NULL) {
$this->name = $name; $this->name = $name;
if (!is_null($content)) {
$this->content = $content;
}
if (!empty($type)) {
$this->contentType = $type;
}
} }
/** /**

View File

@@ -0,0 +1,11 @@
<?php
/**
* @file
*/
namespace HPCloud\Transport;
/**
* Represents an HTTP 412 error.
*
* During some PUT requests, Content-Length is a required header.
*/
class LengthRequiredException extends \HPCloud\Exception {}

View File

@@ -99,6 +99,8 @@ class PHPStreamTransport implements Transporter {
case '403': case '403':
case '401': case '401':
throw new \HPCloud\Transport\AuthorizationException($matches[0]); throw new \HPCloud\Transport\AuthorizationException($matches[0]);
case '412':
throw new \HPCloud\Transport\LengthRequiredException($matches[0]);
case '404': case '404':
throw new \HPCloud\Transport\FileNotFoundException($matches[0]); throw new \HPCloud\Transport\FileNotFoundException($matches[0]);
case '500': case '500':

View File

@@ -0,0 +1,12 @@
<?php
/**
* @file
*/
namespace HPCloud\Transport;
/**
* Represents an HTTP 422 error.
*
* This often represents a case where a checksum or hash did not match
* the generated checksum on the remote end.
*/
class UnprocessableEntityException extends \HPCloud\Exception {}

View File

@@ -16,10 +16,6 @@ class ContainerTest extends \HPCloud\Tests\TestCase {
const FILENAME = 'unit-test-dummy.txt'; const FILENAME = 'unit-test-dummy.txt';
const FILESTR = 'This is a test.'; const FILESTR = 'This is a test.';
protected function liveContainerFixture() {
}
// The factory functions (newFrom*) are tested in the // The factory functions (newFrom*) are tested in the
// ObjectStorage tests, as they are required there. // ObjectStorage tests, as they are required there.
// Rather than build a Mock to achieve the same test here, // Rather than build a Mock to achieve the same test here,
@@ -44,8 +40,53 @@ class ContainerTest extends \HPCloud\Tests\TestCase {
} }
public function testSave() { /**
* Get a container from the server.
*/
protected function containerFixture() {
$store = $this->swiftAuth();
$cname = self::$settings['hpcloud.swift.container'];
try {
$store->createContainer($cname);
$container = $store->container($cname);
}
// This is why PHP needs 'finally'.
catch (\Exception $e) {
// Delete the container.
$store->deleteContainer($cname);
throw $e;
}
return $container;
}
/**
* Destroy a container fixture.
*
* This should be called in any method that uses containerFixture().
*/
protected function destroyContainerFixture() {
$store = $this->swiftAuth();
$cname = self::$settings['hpcloud.swift.container'];
$store->deleteContainer($cname);
}
public function testSave() {
$container = $this->containerFixture();
$name = __FUNCTION__;
$content = "This is a test.";
$type = 'text/plain';
$obj = new Object($name, $content, $type);
$container->save($obj);
$this->destroyContainerFixture();
} }
public function testContents() { public function testContents() {

View File

@@ -38,6 +38,12 @@ class ObjectTest extends \HPCloud\Tests\TestCase {
$o = $this->basicObjectFixture(); $o = $this->basicObjectFixture();
$this->assertEquals(self::FNAME, $o->name()); $this->assertEquals(self::FNAME, $o->name());
$o = new Object('a', 'b', 'text/plain');
$this->assertEquals('a', $o->name());
$this->assertEquals('b', $o->content());
$this->assertEquals('text/plain', $o->contentType());
} }
public function testContentType() { public function testContentType() {