Skip to content

Commit 9ba33fd

Browse files
committed
Merge pull request square#794 from square/dimitris/local-cach-e
Do not stomp exception when disk cache fails to load
2 parents bb04e97 + 1984409 commit 9ba33fd

File tree

5 files changed

+14
-5
lines changed

5 files changed

+14
-5
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ class BitmapHunter implements Runnable {
116116
dispatcher.dispatchComplete(this);
117117
}
118118
} catch (Downloader.ResponseException e) {
119-
exception = e;
119+
if (!e.localCacheOnly || e.responseCode != 504) {
120+
exception = e;
121+
}
120122
dispatcher.dispatchFailed(this);
121123
} catch (IOException e) {
122124
exception = e;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ public interface Downloader {
4343

4444
/** Thrown for non-2XX responses. */
4545
class ResponseException extends IOException {
46-
public ResponseException(String message) {
46+
final boolean localCacheOnly;
47+
final int responseCode;
48+
49+
public ResponseException(String message, boolean localCacheOnly, int responseCode) {
4750
super(message);
51+
this.localCacheOnly = localCacheOnly;
52+
this.responseCode = responseCode;
4853
}
4954
}
5055

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ protected OkHttpClient getClient() {
105105
int responseCode = connection.getResponseCode();
106106
if (responseCode >= 300) {
107107
connection.disconnect();
108-
throw new ResponseException(responseCode + " " + connection.getResponseMessage());
108+
throw new ResponseException(responseCode + " " + connection.getResponseMessage(),
109+
localCacheOnly, responseCode);
109110
}
110111

111112
String responseSource = connection.getHeaderField(RESPONSE_SOURCE_OKHTTP);

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

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

7071
long contentLength = connection.getHeaderFieldInt("Content-Length", -1);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public class BitmapHunterTest {
117117
@Test public void responseExcpetionDispatchFailed() throws Exception {
118118
Action action = mockAction(URI_KEY_1, URI_1);
119119
BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action, null,
120-
new Downloader.ResponseException("Test"));
120+
new Downloader.ResponseException("Test", false, 504));
121121
hunter.run();
122122
verify(dispatcher).dispatchFailed(hunter);
123123
}

0 commit comments

Comments
 (0)