softlayer / softlayer-object-storage-php

SoftLayer Object Storage PHP Client
Other
28 stars 18 forks source link

delete() throwing exception on 404 instead of returning false #11

Closed jjriv closed 9 years ago

jjriv commented 11 years ago

I'm making the following call: $res = $os->with('container/filename.txt')->delete();

But instead of $res getting set to a boolean value, an exception is being thrown, which is not being caught, so a fatal error occurs:

Error level: 1 Error message: Uncaught exception 'ObjectStorage_Exception_Http_NotFound' with message 'Not Found' in /var/www/vhosts/jjriv.assignthis.com/app/LocalLib/ObjectStorage/ObjectStorage.php:531 Stack trace:

0 /var/www/vhosts/jjriv.assignthis.com/app/LocalLib/ObjectStorage/ObjectStorage/Exception/Http.php(10): ObjectStorage_Exception_Http->throwException(404, NULL, '/var/www/vhosts...', 531)

1 /var/www/vhosts/jjriv.assignthis.com/app/LocalLib/ObjectStorage/ObjectStorage.php(531): ObjectStorage_Exception_Http->__construct(NULL, 404)

2 /var/www/vhosts/jjriv.assignthis.com/app/LocalLib/ObjectStorage/ObjectStorage/Abstract.php(607): ObjectStorage->delete(Object(ObjectStorage_Object))

3 /var/www/vhosts/jjriv.assignthis.com/app/cron/upload_garbage_collection.php(56): ObjectStorage_Abstract->delete()

But in the unit test docs, the delete() is not performed inside a try/catch clause: https://github.com/softlayer/softlayer-object-storage-php/blob/master/tests/ObjectTest.php

And according to the Openstack documentation, 404 is a valid response for when the object does not exist: http://docs.openstack.org/api/openstack-object-storage/1.0/content/delete-object.html

Do I need to put my delete() code inside a try/catch or is this a bug? Thanks!

its-clee commented 11 years ago

Hello,

As you pointed out 404 response is expected when a DELETE request is performed on an object that doesn't exist. I'd suggest you wrap the delete call with a try/catch block.

The unit test expects the delete operation to be successful. I wanted the test to fail if the delete operation is not successful. Sorry about the confusion though.

Cheers!

jjriv commented 11 years ago

Thanks for getting back to me and for the confirmation. I went ahead and wrapped the delete call, along with all other calls to ObjectStorage, inside a try/catch block. This way it won't cause a fatal error within my web application. One last question. Once I've caught the 404, or whatever the exception might be, is there a way to get the status code that was received? Something like $e->getStatusCode() where $e is an Exception object?

its-clee commented 11 years ago

I think the ability to get the HTTP code from an exception is a great idea. I will implement the exception code for all HTTP exception sub classes so you can use $e->getCode(). Also, I will update all ObjectStorage_ExceptionHttp* classes to implement ObjectStorage_Exception_Http_Interface so you can expect the HTTP status code when you're catching these exceptions.

jjriv commented 11 years ago

Wow! That would be very helpful. Thanks! I'll look forward to the update.

its-clee commented 11 years ago

HTTP exceptions will have the status code now.

Cheers!

CrackerJackMack commented 11 years ago

Probably needs docs in the README

briancline commented 9 years ago

Going ahead and closing this since it looks like it was resolved with c65981b8. Thanks!