Skip to content

Commit cb27ff6

Browse files
Fixed NPE on GCSSource when bucket does not exist
1 parent d6dbdb7 commit cb27ff6

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

src/main/java/io/cdap/plugin/gcp/gcs/source/GCSSource.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.cdap.plugin.gcp.gcs.source;
1818

1919
import com.google.auth.Credentials;
20+
import com.google.cloud.storage.Bucket;
2021
import com.google.cloud.storage.Storage;
2122
import com.google.cloud.storage.StorageException;
2223
import com.google.common.base.Strings;
@@ -55,6 +56,7 @@
5556
import java.util.HashMap;
5657
import java.util.List;
5758
import java.util.Map;
59+
import java.util.Objects;
5860
import java.util.regex.Pattern;
5961
import javax.annotation.Nullable;
6062

@@ -119,16 +121,24 @@ public void prepareRun(BatchSourceContext context) throws Exception {
119121
}
120122

121123
Storage storage = GCPUtils.getStorage(config.connection.getProject(), credentials);
124+
Bucket bucket = storage.get(bucketName);
125+
if (Objects.isNull(bucket)) {
126+
String errorReason = String.format("Unable to access GCS bucket '%s'.",
127+
bucketName);
128+
collector.addFailure(String.format("%s Ensure you entered the correct bucket path.", errorReason),
129+
null);
130+
collector.getOrThrowException();
131+
}
122132
String location = null;
123133
try {
124134
// Get location of the source for lineage
125-
location = storage.get(bucketName).getLocation();
135+
location = bucket.getLocation();
126136
} catch (StorageException e) {
127137
String errorReason = String.format("Error code: %s, Unable to access GCS bucket '%s'. ",
128-
e.getCode(), bucketName);
138+
e.getCode(), bucketName);
129139
collector.addFailure(String.format("%s %s", errorReason, e.getMessage()),
130-
"Ensure you entered the correct bucket path and have permissions for it.")
131-
.withStacktrace(e.getStackTrace());
140+
"Ensure you entered the correct bucket path and have permissions for it.")
141+
.withStacktrace(e.getStackTrace());
132142
collector.getOrThrowException();
133143
}
134144

0 commit comments

Comments
 (0)