How are you suppose to detect a 404 from an IO exception. I could just search the error message for "404", but is that the correct way? Anything more direct?
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.Drive.Files.Update;
import com.google.api.services.drive.Drive;
File result = null;
try {
update = drive.files().update(driveId, file , mediaContent);
update.setNewRevision(true);
result = update.execute();
} catch (IOException e) {
Log.e(TAG, "file update exception, statusCode: " + update.getLastStatusCode());
Log.e(TAG, "file update exception, e: " + e.getMessage());
}
Log.e(TAG, "file update exception, statuscode " + update.getLastStatusCode());
03-03 05:04:31.738: E/System.out(31733): file update exception, statusCode: -1
03-03 05:04:31.738: E/System.out(31733): file update exception, e: 404 Not Found
03-03 05:04:31.738: E/System.out(31733): "message": "File not found: FileIdRemoved",
Answer: Aegan's comment below was correct, turns out you can subclass the the exception to a GoogleJsonResponseException and from there get the status code. The answer in this case ultimately depended on the fact I am using a GoogleClient, which generates a subclass of IO Exception that contains the status code.
Example:
Try{
...
}catch (IOException e) {
if(e instanceof GoogleJsonResponseException){
int statusCode = ((GoogleJsonResponseException) e).getStatusCode();
//do something
}
}
Handle HttpResponseException
:
catch (HttpResponseException hre) {
if (hre.getStatusCode() == 404) {
// TODO: Handle Http 404
}
}
Detail:
AbstractGoogleClientRequest
creates exceptions.See source code
execute
method calls executeUnparsed
. executeUnparsed
creates exception with newExceptionOnError
. There you will see, it throws a HttpResponseException
(which is a subclass of IOException
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With