Skip to content

Commit 70e8075

Browse files
Adding more sympathetic error handling when no objects found
1 parent 3dddcec commit 70e8075

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

lib/OpenCloud/ObjectStore/AbstractService.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,4 @@ public function getAccount()
4545
{
4646
return new Resource\Account($this);
4747
}
48-
4948
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace OpenCloud\ObjectStore\Exception;
4+
5+
class ObjectNotFoundException extends \RuntimeException
6+
{
7+
public static function factory($name, \Exception $exception)
8+
{
9+
$message = sprintf(
10+
"%s could not be found. The API returned this HTTP response:\n\n%s",
11+
$name,
12+
(string) $exception->getResponse()
13+
);
14+
15+
$e = new self($message);
16+
17+
$e->name = $name;
18+
$e->response = $exception->getResponse();
19+
$e->request = $exception->getRequest();
20+
21+
return $e;
22+
}
23+
}

lib/OpenCloud/ObjectStore/Resource/Container.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace OpenCloud\ObjectStore\Resource;
1212

1313
use Guzzle\Http\EntityBody;
14+
use Guzzle\Http\Exception\BadResponseException;
1415
use Guzzle\Http\Exception\ClientErrorResponseException;
1516
use Guzzle\Http\Message\Response;
1617
use Guzzle\Http\Url;
@@ -19,6 +20,7 @@
1920
use OpenCloud\Common\Service\ServiceInterface;
2021
use OpenCloud\ObjectStore\Constants\Header as HeaderConst;
2122
use OpenCloud\ObjectStore\Exception\ContainerException;
23+
use OpenCloud\ObjectStore\Exception\ObjectNotFoundException;
2224
use OpenCloud\ObjectStore\Upload\DirectorySync;
2325
use OpenCloud\ObjectStore\Upload\TransferBuilder;
2426

@@ -322,9 +324,16 @@ public function dataObject($info = null)
322324
*/
323325
public function getObject($name, array $headers = array())
324326
{
325-
$response = $this->getClient()
326-
->get($this->getUrl($name), $headers)
327-
->send();
327+
try {
328+
$response = $this->getClient()
329+
->get($this->getUrl($name), $headers)
330+
->send();
331+
} catch (BadResponseException $e) {
332+
if ($e->getResponse()->getStatusCode() == 404) {
333+
throw ObjectNotFoundException::factory($name, $e);
334+
}
335+
throw $e;
336+
}
328337

329338
return $this->dataObject()
330339
->populateFromResponse($response)

tests/OpenCloud/Tests/ObjectStore/Resource/ContainerTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace OpenCloud\Tests\ObjectStore\Resource;
1212

13+
use Guzzle\Http\Message\Response;
1314
use OpenCloud\Common\Constants\Size;
1415
use OpenCloud\Tests\ObjectStore\ObjectStoreTestCase;
1516

@@ -148,7 +149,25 @@ public function test_Get_Object()
148149
);
149150
$this->assertEquals('foobar', $object->getName());
150151
}
151-
152+
153+
/**
154+
* @expectedException \OpenCloud\ObjectStore\Exception\ObjectNotFoundException
155+
*/
156+
public function test_Get_Object_404()
157+
{
158+
$this->addMockSubscriber(new Response(404));
159+
$this->container->getObject('foobar');
160+
}
161+
162+
/**
163+
* @expectedException \Guzzle\Http\Exception\BadResponseException
164+
*/
165+
public function test_Get_Object_500()
166+
{
167+
$this->addMockSubscriber(new Response(500));
168+
$this->container->getObject('foobar');
169+
}
170+
152171
/**
153172
* @expectedException OpenCloud\Common\Exceptions\InvalidArgumentError
154173
*/

tests/OpenCloud/Tests/ObjectStore/Resource/DataObjectTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,4 @@ public function test_Public_Urls()
9494
$this->assertNotNull($object->getPublicUrl(UrlType::STREAMING));
9595
$this->assertNotNull($object->getPublicUrl(UrlType::IOS_STREAMING));
9696
}
97-
9897
}

0 commit comments

Comments
 (0)