Skip to content

Commit

Permalink
Include full key in Azure read operations exceptions (#75920)
Browse files Browse the repository at this point in the history
Closes #75916
  • Loading branch information
fcofdez authored Aug 2, 2021
1 parent f355d93 commit 09a5db3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
Expand All @@ -40,6 +41,7 @@
import java.util.regex.Pattern;

import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

@SuppressForbidden(reason = "this test uses a HttpServer to emulate an Azure endpoint")
Expand Down Expand Up @@ -243,4 +245,12 @@ public void testDeleteBlobsIgnoringIfNotExists() throws Exception {
assertThat(container.listBlobs(), is(anEmptyMap()));
}
}

public void testNotFoundErrorMessageContainsFullKey() throws Exception {
try (BlobStore store = newBlobStore()) {
BlobContainer container = store.blobContainer(BlobPath.EMPTY.add("nested").add("dir"));
NoSuchFileException exception = expectThrows(NoSuchFileException.class, () -> container.readBlob("blob"));
assertThat(exception.getMessage(), equalTo("Blob [nested/dir/blob] not found"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public boolean blobExists(String blobName) throws IOException {
}

private InputStream openInputStream(String blobName, long position, @Nullable Long length) throws IOException {
String blobKey = buildKey(blobName);
logger.trace("readBlob({}) from position [{}] with length [{}]", blobName, position, length != null ? length : "unlimited");
if (blobStore.getLocationMode() == LocationMode.SECONDARY_ONLY && blobExists(blobName) == false) {
// On Azure, if the location path is a secondary location, and the blob does not
Expand All @@ -55,18 +56,18 @@ private InputStream openInputStream(String blobName, long position, @Nullable Lo
// before throwing a storage exception. This can cause long delays in retrieving
// snapshots, so we first check if the blob exists before trying to open an input
// stream to it.
throw new NoSuchFileException("Blob [" + blobName + "] does not exist");
throw new NoSuchFileException("Blob [" + blobKey + "] not found");
}
try {
return blobStore.getInputStream(buildKey(blobName), position, length);
return blobStore.getInputStream(blobKey, position, length);
} catch (Exception e) {
Throwable rootCause = Throwables.getRootCause(e);
if (rootCause instanceof BlobStorageException) {
if (((BlobStorageException) rootCause).getStatusCode() == 404) {
throw new NoSuchFileException("Blob [" + blobName + "] not found");
throw new NoSuchFileException("Blob [" + blobKey + "] not found");
}
}
throw new IOException("Unable to get input stream for blob [" + blobName + "]", e);
throw new IOException("Unable to get input stream for blob [" + blobKey + "]", e);
}
}

Expand Down

0 comments on commit 09a5db3

Please sign in to comment.