Skip to content

Commit 31f11ec

Browse files
committed
Merge pull request square#363 from square/dimitris/responsex
Non 200 responses now throw exception.
2 parents 1b90852 + fef6698 commit 31f11ec

File tree

10 files changed

+51
-33
lines changed

10 files changed

+51
-33
lines changed

picasso/src/main/java/com/squareup/picasso/BitmapHunter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ protected void setExifRotation(int exifRotation) {
8989
} else {
9090
dispatcher.dispatchComplete(this);
9191
}
92+
} catch (Downloader.ResponseException e) {
93+
exception = e;
94+
dispatcher.dispatchFailed(this);
9295
} catch (IOException e) {
9396
exception = e;
9497
dispatcher.dispatchRetry(this);

picasso/src/main/java/com/squareup/picasso/ContactsPhotoBitmapHunter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import android.graphics.BitmapFactory;
2323
import android.net.Uri;
2424
import android.provider.ContactsContract;
25-
2625
import java.io.IOException;
2726
import java.io.InputStream;
2827

picasso/src/main/java/com/squareup/picasso/Downloader.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ public interface Downloader {
3535
*/
3636
Response load(Uri uri, boolean localCacheOnly) throws IOException;
3737

38+
/** Thrown for non 200 responses. */
39+
class ResponseException extends IOException {
40+
public ResponseException(String message) {
41+
super(message);
42+
}
43+
}
44+
3845
/** Response stream or bitmap and info. */
3946
class Response {
4047
final InputStream stream;

picasso/src/main/java/com/squareup/picasso/OkHttpDownloader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected OkHttpClient getClient() {
105105
int responseCode = connection.getResponseCode();
106106
if (responseCode >= 300) {
107107
connection.disconnect();
108-
return null;
108+
throw new ResponseException(responseCode + " " + connection.getResponseMessage());
109109
}
110110

111111
String responseSource = connection.getHeaderField(RESPONSE_SOURCE_OKHTTP);

picasso/src/main/java/com/squareup/picasso/Picasso.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
import java.util.concurrent.ExecutorService;
3333

3434
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
35+
import static com.squareup.picasso.Action.RequestWeakReference;
3536
import static com.squareup.picasso.Dispatcher.HUNTER_BATCH_COMPLETE;
3637
import static com.squareup.picasso.Dispatcher.REQUEST_GCED;
37-
import static com.squareup.picasso.Action.RequestWeakReference;
3838
import static com.squareup.picasso.Utils.THREAD_PREFIX;
3939

4040
/**

picasso/src/main/java/com/squareup/picasso/UrlConnectionDownloader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected HttpURLConnection openConnection(Uri path) throws IOException {
6464
int responseCode = connection.getResponseCode();
6565
if (responseCode >= 300) {
6666
connection.disconnect();
67-
return null;
67+
throw new ResponseException(responseCode + " " + connection.getResponseMessage());
6868
}
6969

7070
boolean fromCache = parseResponseSourceHeader(connection.getHeaderField(RESPONSE_SOURCE));

picasso/src/main/java/com/squareup/picasso/Utils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import android.os.Process;
2626
import android.os.StatFs;
2727
import android.provider.Settings;
28-
2928
import java.io.ByteArrayOutputStream;
3029
import java.io.File;
3130
import java.io.IOException;

picasso/src/test/java/com/squareup/picasso/BitmapHunterTest.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ public class BitmapHunterTest {
100100
verify(dispatcher).dispatchFailed(hunter);
101101
}
102102

103+
@Test public void responseExcpetionDispatchFailed() throws Exception {
104+
Action action = mockAction(URI_KEY_1, URI_1);
105+
BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action, null,
106+
new Downloader.ResponseException("Test"));
107+
hunter.run();
108+
verify(dispatcher).dispatchFailed(hunter);
109+
}
110+
103111
@Test public void outOfMemoryDispatchFailed() throws Exception {
104112
when(stats.createSnapshot()).thenReturn(mock(StatsSnapshot.class));
105113

@@ -119,8 +127,8 @@ public class BitmapHunterTest {
119127

120128
@Test public void runWithIoExceptionDispatchRetry() throws Exception {
121129
Action action = mockAction(URI_KEY_1, URI_1);
122-
BitmapHunter hunter =
123-
new TestableBitmapHunter(picasso, dispatcher, cache, stats, action, null, true);
130+
BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action, null,
131+
new IOException());
124132
hunter.run();
125133
verify(dispatcher).dispatchRetry(hunter);
126134
}
@@ -440,7 +448,7 @@ public class BitmapHunterTest {
440448

441449
private static class TestableBitmapHunter extends BitmapHunter {
442450
private final Bitmap result;
443-
private final boolean throwException;
451+
private final IOException exception;
444452

445453
TestableBitmapHunter(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats,
446454
Action action) {
@@ -449,19 +457,19 @@ private static class TestableBitmapHunter extends BitmapHunter {
449457

450458
TestableBitmapHunter(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats,
451459
Action action, Bitmap result) {
452-
this(picasso, dispatcher, cache, stats, action, result, false);
460+
this(picasso, dispatcher, cache, stats, action, result, null);
453461
}
454462

455463
TestableBitmapHunter(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats,
456-
Action action, Bitmap result, boolean throwException) {
464+
Action action, Bitmap result, IOException exception) {
457465
super(picasso, dispatcher, cache, stats, action);
458466
this.result = result;
459-
this.throwException = throwException;
467+
this.exception = exception;
460468
}
461469

462470
@Override Bitmap decode(Request data) throws IOException {
463-
if (throwException) {
464-
throw new IOException("Failed.");
471+
if (exception != null) {
472+
throw exception;
465473
}
466474
return result;
467475
}

picasso/src/test/java/com/squareup/picasso/OkHttpDownloaderTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static com.squareup.picasso.OkHttpDownloader.RESPONSE_SOURCE_ANDROID;
3434
import static com.squareup.picasso.OkHttpDownloader.RESPONSE_SOURCE_OKHTTP;
3535
import static org.fest.assertions.api.Assertions.assertThat;
36+
import static org.junit.Assert.fail;
3637

3738
@RunWith(RobolectricTestRunner.class)
3839
@Config(manifest = Config.NONE)
@@ -58,16 +59,6 @@ public class OkHttpDownloaderTest {
5859
server.shutdown();
5960
}
6061

61-
@Test public void nonTwoHundredReturnsNull() throws Exception {
62-
server.enqueue(new MockResponse().setResponseCode(302));
63-
server.enqueue(new MockResponse().setResponseCode(404));
64-
server.enqueue(new MockResponse().setResponseCode(500));
65-
66-
assertThat(loader.load(URL, false)).isNull();
67-
assertThat(loader.load(URL, false)).isNull();
68-
assertThat(loader.load(URL, false)).isNull();
69-
}
70-
7162
@Test public void allowExpiredSetsCacheControl() throws Exception {
7263
server.enqueue(new MockResponse());
7364
loader.load(URL, false);
@@ -94,4 +85,14 @@ public class OkHttpDownloaderTest {
9485
Downloader.Response response3 = loader.load(URL, true);
9586
assertThat(response3.cached).isTrue();
9687
}
88+
89+
@Test public void throwsResponseException() throws Exception {
90+
server.enqueue(new MockResponse().setStatus("HTTP/1.1 401 Not Authorized"));
91+
try {
92+
loader.load(URL, false);
93+
fail("Expected ResponseException.");
94+
} catch (Downloader.ResponseException e) {
95+
assertThat(e).hasMessage("401 Not Authorized");
96+
}
97+
}
9798
}

picasso/src/test/java/com/squareup/picasso/UrlConnectionDownloaderTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
3535
import static com.squareup.picasso.UrlConnectionDownloader.RESPONSE_SOURCE;
3636
import static org.fest.assertions.api.Assertions.assertThat;
37+
import static org.junit.Assert.fail;
3738

3839
@RunWith(RobolectricTestRunner.class)
3940
@Config(manifest = Config.NONE)
@@ -59,16 +60,6 @@ public class UrlConnectionDownloaderTest {
5960
server.shutdown();
6061
}
6162

62-
@Test public void nonTwoHundredReturnsNull() throws Exception {
63-
server.enqueue(new MockResponse().setResponseCode(302));
64-
server.enqueue(new MockResponse().setResponseCode(404));
65-
server.enqueue(new MockResponse().setResponseCode(500));
66-
67-
assertThat(loader.load(URL, false)).isNull();
68-
assertThat(loader.load(URL, false)).isNull();
69-
assertThat(loader.load(URL, false)).isNull();
70-
}
71-
7263
@Config(reportSdk = ICE_CREAM_SANDWICH)
7364
@Test public void cacheOnlyInstalledOnce() throws Exception {
7465
UrlConnectionDownloader.cache = null;
@@ -117,4 +108,14 @@ public class UrlConnectionDownloaderTest {
117108
Downloader.Response response2 = loader.load(URL, true);
118109
assertThat(response2.cached).isTrue();
119110
}
111+
112+
@Test public void throwsResponseException() throws Exception {
113+
server.enqueue(new MockResponse().setStatus("HTTP/1.1 401 Not Authorized"));
114+
try {
115+
loader.load(URL, false);
116+
fail("Expected ResponseException.");
117+
} catch (Downloader.ResponseException e) {
118+
assertThat(e).hasMessage("401 Not Authorized");
119+
}
120+
}
120121
}

0 commit comments

Comments
 (0)