Skip to content

Commit 24bad29

Browse files
authored
xuy-UID2-4848-improve-clouddownload-logging #387 (#388)
* xuy-UID2-4848-improve-clouddownload-logging #387
1 parent 2b290cc commit 24bad29

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/main/java/com/uid2/shared/cloud/URLStorageWithMetadata.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.uid2.shared.cloud;
22

33
import java.io.InputStream;
4+
import java.net.HttpURLConnection;
45
import java.net.MalformedURLException;
56
import java.net.Proxy;
67
import java.net.URL;
@@ -35,15 +36,30 @@ public void upload(InputStream input, String cloudPath) throws CloudStorageExcep
3536
public InputStream download(String cloudPath) throws CloudStorageException {
3637
try {
3738
URL url = new URL(cloudPath);
38-
39+
HttpURLConnection httpConn;
40+
3941
if (this.proxy != null) {
40-
return url.openConnection(proxy).getInputStream();
42+
httpConn = (HttpURLConnection) url.openConnection(proxy);
43+
} else {
44+
httpConn = (HttpURLConnection) url.openConnection();
45+
}
46+
47+
int responseCode = httpConn.getResponseCode();
48+
if (responseCode >= 200 && responseCode < 300) {
49+
return httpConn.getInputStream();
4150
} else {
42-
return url.openStream();
51+
throw new CloudStorageException("Cannot download required files, HTTP response code " + responseCode
52+
+ ", please visit UID2 guides for more details");
4353
}
44-
} catch (Throwable t) {
45-
// Do not log the message or the original exception as that may contain the pre-signed url
46-
throw new CloudStorageException("url download error: " + t.getClass().getSimpleName());
54+
}
55+
catch (CloudStorageException e) {
56+
// Directly rethrow without wrapping again
57+
throw e;
58+
}
59+
catch (Throwable t) {
60+
// Do not log the original exception as it may contain sensitive information such as the pre-signed URL
61+
throw new CloudStorageException("Cannot download required files, exception: " + t.getClass().getSimpleName() +
62+
", please visit UID2 guides for more details");
4763
}
4864
}
4965

0 commit comments

Comments
 (0)