Skip to content

Commit 43efdce

Browse files
author
Kannan Goundan
committed
DbxClientV1: Clean up IOException separation.
- Previously, we intercepted all HiddenExceptions. Now, only intercept the instances that were thrown by our wrapper. - Be more precise with our 'try' blocks to make sure the caller's exceptions get all the way through.
1 parent e707252 commit 43efdce

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/main/java/com/dropbox/core/NoThrowOutputStream.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void flush()
4242
underlying.flush();
4343
}
4444
catch (IOException ex) {
45-
throw new HiddenException(ex);
45+
throw new HiddenException(this, ex);
4646
}
4747
}
4848

@@ -54,7 +54,7 @@ public void write(byte[] b, int off, int len)
5454
underlying.write(b, off, len);
5555
}
5656
catch (IOException ex) {
57-
throw new HiddenException(ex);
57+
throw new HiddenException(this, ex);
5858
}
5959
}
6060

@@ -66,7 +66,7 @@ public void write(byte[] b)
6666
underlying.write(b);
6767
}
6868
catch (IOException ex) {
69-
throw new HiddenException(ex);
69+
throw new HiddenException(this, ex);
7070
}
7171
}
7272

@@ -78,18 +78,24 @@ public void write(int b)
7878
underlying.write(b);
7979
}
8080
catch (IOException ex) {
81-
throw new HiddenException(ex);
81+
throw new HiddenException(this, ex);
8282
}
8383
}
8484

8585
public static final class HiddenException extends RuntimeException
8686
{
87-
public final IOException underlying;
87+
public final NoThrowOutputStream owner;
8888

89-
public HiddenException(IOException underlying)
89+
public HiddenException(NoThrowOutputStream owner, IOException underlying)
9090
{
9191
super(underlying);
92-
this.underlying = underlying;
92+
this.owner = owner;
93+
}
94+
95+
@Override
96+
public IOException getCause()
97+
{
98+
return (IOException) super.getCause();
9399
}
94100

95101
public static final long serialVersionUID = 0;

src/main/java/com/dropbox/core/v1/DbxClientV1.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,8 @@ public <E extends Throwable> DbxEntry.File finishUploadFile(Uploader uploader, D
718718
return uploader.finish();
719719
}
720720
catch (NoThrowOutputStream.HiddenException ex) {
721-
// We hid our OutputStream's IOException from their writer.write() function so that
722-
// we could properly raise a NetworkIOException if something went wrong with the
723-
// network stream.
724-
throw new NetworkIOException(ex.underlying);
721+
if (ex.owner == streamWrapper) throw new NetworkIOException(ex.getCause());
722+
throw ex;
725723
}
726724
finally {
727725
uploader.close();
@@ -940,21 +938,29 @@ private <E extends Throwable> HttpRequestor.Response chunkedUploadCommon(String[
940938

941939
HttpRequestor.Uploader uploader = DbxRequestUtil.startPut(requestConfig, accessToken, USER_AGENT_ID, host.getContent(), apiPath, params, headers);
942940
try {
941+
NoThrowOutputStream nt = new NoThrowOutputStream(uploader.getBody());
943942
try {
944-
NoThrowOutputStream nt = new NoThrowOutputStream(uploader.getBody());
943+
// Any exceptions thrown by writer.write (e.g. IOException) are "owned" by
944+
// the caller so let them propagate. We should only handle exceptions caused
945+
// by our uploader OutputStream.
945946
writer.write(nt);
946-
long bytesWritten = nt.getBytesWritten();
947-
if (bytesWritten != chunkSize) {
948-
throw new IllegalStateException("'chunkSize' is " + chunkSize + ", but 'writer' only wrote " + bytesWritten + " bytes");
949-
}
947+
}
948+
catch (NoThrowOutputStream.HiddenException ex) {
949+
if (ex.owner == nt) throw new NetworkIOException(ex.getCause());
950+
throw ex;
951+
}
952+
953+
long bytesWritten = nt.getBytesWritten();
954+
if (bytesWritten != chunkSize) {
955+
throw new IllegalStateException("'chunkSize' is " + chunkSize + ", but 'writer' only wrote " + bytesWritten + " bytes");
956+
}
957+
958+
try {
950959
return uploader.finish();
951960
}
952961
catch (IOException ex) {
953962
throw new NetworkIOException(ex);
954963
}
955-
catch (NoThrowOutputStream.HiddenException ex) {
956-
throw new NetworkIOException(ex.underlying);
957-
}
958964
}
959965
finally {
960966
uploader.close();

0 commit comments

Comments
 (0)