Skip to content

Commit 6f631ea

Browse files
committed
Centralize response handling and respect returnError param.
1 parent 95fabe9 commit 6f631ea

File tree

5 files changed

+59
-90
lines changed

5 files changed

+59
-90
lines changed

cloudinary-android/src/main/java/com/cloudinary/android/UploaderStrategy.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,7 @@ public void totalBytesLoaded(long bytes) {
119119
responseStream.close();
120120
} catch (Exception e) {}
121121

122-
if (code != 200 && code != 400 && code != 404 && code != 500) {
123-
throw new RuntimeException("Server returned unexpected status code - " + code + " - " + responseData);
124-
}
125-
126-
JSONObject result;
127-
try {
128-
result = new JSONObject(responseData);
129-
if (result.has("error")) {
130-
JSONObject error = result.getJSONObject("error");
131-
if (returnError) {
132-
error.put("http_code", code);
133-
} else {
134-
throw new RuntimeException(error.getString("message"));
135-
}
136-
}
137-
return ObjectUtils.toMap(result);
138-
} catch (JSONException e) {
139-
throw new RuntimeException("Invalid JSON response from server " + e.getMessage());
140-
}
122+
return processResponse(returnError, code, responseData);
141123
}
142124

143125
private long determineLength(Object file) {

cloudinary-core/src/main/java/com/cloudinary/strategies/AbstractUploaderStrategy.java

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package com.cloudinary.strategies;
22

3-
import java.io.IOException;
4-
import java.util.Map;
5-
63
import com.cloudinary.Cloudinary;
74
import com.cloudinary.ProgressCallback;
85
import com.cloudinary.Uploader;
96
import com.cloudinary.utils.ObjectUtils;
107
import com.cloudinary.utils.StringUtils;
8+
import org.cloudinary.json.JSONException;
9+
import org.cloudinary.json.JSONObject;
10+
11+
import java.io.IOException;
12+
import java.util.Arrays;
13+
import java.util.HashMap;
14+
import java.util.Map;
1115

1216
public abstract class AbstractUploaderStrategy {
17+
private final static int[] ERROR_CODES = new int[]{400, 401, 403, 404, 420, 500};
1318
protected Uploader uploader;
1419

1520
public void init(Uploader uploader) {
@@ -43,6 +48,51 @@ protected String buildUploadUrl(String action, Map options) {
4348
}
4449
}
4550

51+
protected Map processResponse(boolean returnError, int code, String responseData) {
52+
String errorMessage = null;
53+
Map result = null;
54+
if (code == 200 || includesServerResponse(code)) {
55+
try {
56+
JSONObject responseJSON = new JSONObject(responseData);
57+
result = ObjectUtils.toMap(responseJSON);
58+
59+
if (result.containsKey("error")) {
60+
Map error = (Map) result.get("error");
61+
error.put("http_code", code);
62+
errorMessage = (String) error.get("message");
63+
}
64+
} catch (JSONException e) {
65+
errorMessage = "Invalid JSON response from server " + e.getMessage();
66+
}
67+
} else {
68+
errorMessage = "Server returned unexpected status code - " + code;
69+
if (StringUtils.isNotBlank(responseData)) {
70+
errorMessage += (" - " + responseData);
71+
}
72+
}
73+
74+
if (StringUtils.isNotBlank(errorMessage)) {
75+
if (returnError) {
76+
// return a result containing the error instead of throwing an exception:
77+
if (result == null) {
78+
Map error = new HashMap();
79+
error.put("http_code", code);
80+
error.put("message", errorMessage);
81+
result = new HashMap();
82+
result.put("error", error);
83+
} // else - Result is already built, with the error inside. Nothing to do.
84+
} else {
85+
throw new RuntimeException(errorMessage);
86+
}
87+
}
88+
89+
return result;
90+
}
91+
92+
private boolean includesServerResponse(int code) {
93+
return Arrays.binarySearch(ERROR_CODES, code) >= 0;
94+
}
95+
4696
protected boolean requiresSigning(String action, Map options) {
4797
boolean unsigned = Boolean.TRUE.equals(options.get("unsigned"));
4898
boolean deleteByToken = "delete_by_token".equals(action);

cloudinary-http42/src/main/java/com/cloudinary/http42/UploaderStrategy.java

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.cloudinary.http42;
22

33
import com.cloudinary.Cloudinary;
4+
import com.cloudinary.ProgressCallback;
45
import com.cloudinary.Util;
56
import com.cloudinary.strategies.AbstractUploaderStrategy;
6-
import com.cloudinary.ProgressCallback;
77
import com.cloudinary.utils.ObjectUtils;
88
import com.cloudinary.utils.StringUtils;
99
import org.apache.http.HttpHost;
@@ -18,8 +18,6 @@
1818
import org.apache.http.entity.mime.content.FileBody;
1919
import org.apache.http.entity.mime.content.StringBody;
2020
import org.apache.http.impl.client.DefaultHttpClient;
21-
import org.cloudinary.json.JSONException;
22-
import org.cloudinary.json.JSONObject;
2321

2422
import java.io.File;
2523
import java.io.IOException;
@@ -115,28 +113,7 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
115113
InputStream responseStream = response.getEntity().getContent();
116114
String responseData = StringUtils.read(responseStream);
117115

118-
if (code != 200 && code != 400 && code != 404 && code != 500) {
119-
throw new RuntimeException("Server returned unexpected status code - " + code + " - " + responseData);
120-
}
121-
122-
Map result;
123-
124-
try {
125-
JSONObject responseJSON = new JSONObject(responseData);
126-
result = ObjectUtils.toMap(responseJSON);
127-
} catch (JSONException e) {
128-
throw new RuntimeException("Invalid JSON response from server " + e.getMessage());
129-
}
130-
131-
if (result.containsKey("error")) {
132-
Map error = (Map) result.get("error");
133-
if (returnError) {
134-
error.put("http_code", code);
135-
} else {
136-
throw new RuntimeException((String) error.get("message"));
137-
}
138-
}
139-
return result;
116+
return processResponse(returnError, code, responseData);
140117
}
141118

142119
}

cloudinary-http43/src/main/java/com/cloudinary/http43/UploaderStrategy.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,28 +136,7 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
136136
response.close();
137137
}
138138

139-
if (code != 200 && code != 400 && code != 404 && code != 500) {
140-
throw new RuntimeException("Server returned unexpected status code - " + code + " - " + responseData);
141-
}
142-
143-
Map result;
144-
145-
try {
146-
JSONObject responseJSON = new JSONObject(responseData);
147-
result = ObjectUtils.toMap(responseJSON);
148-
} catch (JSONException e) {
149-
throw new RuntimeException("Invalid JSON response from server " + e.getMessage());
150-
}
151-
152-
if (result.containsKey("error")) {
153-
Map error = (Map) result.get("error");
154-
if (returnError) {
155-
error.put("http_code", code);
156-
} else {
157-
throw new RuntimeException((String) error.get("message"));
158-
}
159-
}
160-
return result;
139+
return processResponse(returnError, code, responseData);
161140
}
162141

163142
}

cloudinary-http44/src/main/java/com/cloudinary/http44/UploaderStrategy.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.io.InputStream;
6+
import java.lang.reflect.Field;
67
import java.util.Collection;
78
import java.util.Map;
89

@@ -136,27 +137,7 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
136137
response.close();
137138
}
138139

139-
if (code != 200 && code != 400 && code != 404 && code != 500) {
140-
throw new RuntimeException("Server returned unexpected status code - " + code + " - " + responseData);
141-
}
142-
143-
Map result;
144-
145-
try {
146-
JSONObject responseJSON = new JSONObject(responseData);
147-
result = ObjectUtils.toMap(responseJSON);
148-
} catch (JSONException e) {
149-
throw new RuntimeException("Invalid JSON response from server " + e.getMessage());
150-
}
151-
152-
if (result.containsKey("error")) {
153-
Map error = (Map) result.get("error");
154-
if (returnError) {
155-
error.put("http_code", code);
156-
} else {
157-
throw new RuntimeException((String) error.get("message"));
158-
}
159-
}
140+
Map result = processResponse(returnError, code, responseData);
160141
return result;
161142
}
162143
}

0 commit comments

Comments
 (0)