Skip to content

Commit

Permalink
Consolidate azureblob-sdk error handling
Browse files Browse the repository at this point in the history
References #606.
  • Loading branch information
gaul committed Nov 11, 2024
1 parent d8350b5 commit 9e2c40a
Showing 1 changed file with 49 additions and 45 deletions.
94 changes: 49 additions & 45 deletions src/main/java/org/gaul/s3proxy/azureblob/AzureBlobStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ public PageSet<? extends StorageMetadata> list(String container,
options.getDelimiter(), azureOptions, /*timeout=*/ null)
.iterableByPage(marker).iterator().next();
} catch (BlobStorageException bse) {
if (bse.getErrorCode() == BlobErrorCode.CONTAINER_NOT_FOUND) {
throw new ContainerNotFoundException(container, "");
}
translateAndRethrowException(bse, container, /*key=*/ null);
throw bse;
}
for (var blob : page.getValue()) {
Expand Down Expand Up @@ -225,10 +223,7 @@ public boolean createContainerInLocation(Location location,
default: return false;
}
} catch (BlobStorageException bse) {
if (bse.getErrorCode() == BlobErrorCode.INVALID_RESOURCE_NAME) {
throw new IllegalArgumentException(
"Invalid container name", bse);
}
translateAndRethrowException(bse, container, /*key=*/ null);
throw bse;
}
}
Expand Down Expand Up @@ -310,22 +305,8 @@ public Blob getBlob(String container, String key, GetOptions options) {
try {
blobStream = client.openInputStream(azureRange, conditions);
} catch (BlobStorageException bse) {
var code = bse.getErrorCode();
if (code == BlobErrorCode.BLOB_NOT_FOUND) {
throw new KeyNotFoundException(container, key, "");
} else if (code == BlobErrorCode.CONTAINER_NOT_FOUND) {
throw new ContainerNotFoundException(container, "");
} else if (code == BlobErrorCode.CONDITION_NOT_MET) {
var request = HttpRequest.builder()
.method("GET")
.endpoint(endpoint)
.build();
var response = HttpResponse.builder()
.statusCode(Status.PRECONDITION_FAILED.getStatusCode())
.build();
throw new HttpResponseException(
new HttpCommand(request), response);
} else if (bse.getStatusCode() ==
translateAndRethrowException(bse, container, key);
if (bse.getStatusCode() ==
Status.REQUESTED_RANGE_NOT_SATISFIABLE.getStatusCode()) {
throw new HttpResponseException(
"illegal range: " + azureRange, null,
Expand Down Expand Up @@ -421,22 +402,8 @@ public String putBlob(String container, Blob blob, PutOptions options) {
} catch (IOException ioe) {
var cause = ioe.getCause();
if (cause != null && cause instanceof BlobStorageException) {
var bse = (BlobStorageException) cause;
if (bse.getErrorCode() == BlobErrorCode.CONTAINER_NOT_FOUND) {
throw new ContainerNotFoundException(container, "");
} else if (bse.getErrorCode() ==
BlobErrorCode.INVALID_OPERATION) {
var response =
HttpResponse.builder().statusCode(400).build();
var exception = new HttpResponseException(
new HttpCommand(HttpRequest.builder()
.method("GET")
.endpoint("http://stub")
.build()),
response);
exception.initCause(bse);
throw exception;
}
translateAndRethrowException(
(BlobStorageException) cause, container, /*key=*/ null);
}
throw new RuntimeException(ioe);
}
Expand Down Expand Up @@ -527,9 +494,7 @@ public BlobMetadata blobMetadata(String container, String key) {
try {
properties = client.getProperties();
} catch (BlobStorageException bse) {
if (bse.getErrorCode() == BlobErrorCode.BLOB_NOT_FOUND) {
throw new KeyNotFoundException(container, key, "");
}
translateAndRethrowException(bse, container, /*key=*/ null);
throw bse;
}
return new BlobMetadataImpl(/*id=*/ null, key, /*location=*/ null,
Expand All @@ -556,9 +521,7 @@ public ContainerAccess getContainerAccess(String container) {
ContainerAccess.PUBLIC_READ :
ContainerAccess.PRIVATE;
} catch (BlobStorageException bse) {
if (bse.getErrorCode() == BlobErrorCode.CONTAINER_NOT_FOUND) {
throw new ContainerNotFoundException(container, "");
}
translateAndRethrowException(bse, container, /*key=*/ null);
throw bse;
}
}
Expand Down Expand Up @@ -749,4 +712,45 @@ private static ContentMetadata toContentMetadata(
private static String makeBlockId(int partNumber) {
return Base64.getEncoder().encodeToString(Ints.toByteArray(partNumber));
}

/**
* Translate BlobStorageException to a jclouds exception. Throws if
* translated otherwise returns.
*/
private void translateAndRethrowException(BlobStorageException bse,
String container, @Nullable String key) {
var code = bse.getErrorCode();
if (code == BlobErrorCode.BLOB_NOT_FOUND) {
var exception = new KeyNotFoundException(container, key, "");
exception.initCause(bse);
throw exception;
} else if (code == BlobErrorCode.CONTAINER_NOT_FOUND) {
var exception = new ContainerNotFoundException(container, "");
exception.initCause(bse);
throw exception;
} else if (code == BlobErrorCode.CONDITION_NOT_MET) {
var request = HttpRequest.builder()
.method("GET")
.endpoint(endpoint)
.build();
var response = HttpResponse.builder()
.statusCode(Status.PRECONDITION_FAILED.getStatusCode())
.build();
throw new HttpResponseException(
new HttpCommand(request), response, bse);
} else if (code == BlobErrorCode.INVALID_OPERATION) {
var request = HttpRequest.builder()
.method("GET")
.endpoint(endpoint)
.build();
var response = HttpResponse.builder()
.statusCode(Status.BAD_REQUEST.getStatusCode())
.build();
throw new HttpResponseException(
new HttpCommand(request), response, bse);
} else if (bse.getErrorCode() == BlobErrorCode.INVALID_RESOURCE_NAME) {
throw new IllegalArgumentException(
"Invalid container name", bse);
}
}
}

0 comments on commit 9e2c40a

Please sign in to comment.