|
11 | 11 | import java.io.IOException;
|
12 | 12 | import java.io.UnsupportedEncodingException;
|
13 | 13 | import java.net.URLEncoder;
|
| 14 | +import java.net.SocketTimeoutException; |
14 | 15 | import java.nio.charset.StandardCharsets;
|
15 | 16 | import java.util.HashMap;
|
16 | 17 | import java.util.Iterator;
|
@@ -202,22 +203,35 @@ private void getService(String requestUrl) throws IOException {
|
202 | 203 | requestUrl = request.url().toString();
|
203 | 204 | }
|
204 | 205 |
|
205 |
| - Response<ResponseBody> response = this.service.getRequest(requestUrl, this.headers).execute(); |
206 |
| - if (response.isSuccessful()) { |
207 |
| - assert response.body() != null; |
208 |
| - if (request != null) { |
209 |
| - response = pluginResponseImp(request, response); |
210 |
| - } |
211 |
| - responseJSON = new JSONObject(response.body().string()); |
212 |
| - if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) { |
213 |
| - handleJSONArray(); |
| 206 | + try { |
| 207 | + Response<ResponseBody> response = this.service.getRequest(requestUrl, this.headers).execute(); |
| 208 | + if (response.isSuccessful()) { |
| 209 | + assert response.body() != null; |
| 210 | + if (request != null) { |
| 211 | + response = pluginResponseImp(request, response); |
| 212 | + } |
| 213 | + String responseBody = response.body().string(); |
| 214 | + try { |
| 215 | + responseJSON = new JSONObject(responseBody); |
| 216 | + if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) { |
| 217 | + handleJSONArray(); |
| 218 | + } |
| 219 | + connectionRequest.onRequestFinished(CSHttpConnection.this); |
| 220 | + } catch (JSONException e) { |
| 221 | + // Handle non-JSON response |
| 222 | + setError("Invalid JSON response: " + responseBody); |
| 223 | + } |
| 224 | + } else { |
| 225 | + assert response.errorBody() != null; |
| 226 | + setError(response.errorBody().string()); |
214 | 227 | }
|
215 |
| - connectionRequest.onRequestFinished(CSHttpConnection.this); |
216 |
| - } else { |
217 |
| - assert response.errorBody() != null; |
218 |
| - setError(response.errorBody().string()); |
| 228 | + } catch (SocketTimeoutException e) { |
| 229 | + // Handle timeout |
| 230 | + setError("Request timed out: " + e.getMessage()); |
| 231 | + } catch (IOException e) { |
| 232 | + // Handle other IO exceptions |
| 233 | + setError("IO error occurred: " + e.getMessage()); |
219 | 234 | }
|
220 |
| - |
221 | 235 | }
|
222 | 236 |
|
223 | 237 | private Request pluginRequestImp(String requestUrl) {
|
@@ -261,7 +275,13 @@ void handleJSONObject(JSONArray arrayEntry, JSONObject jsonObj, int idx) {
|
261 | 275 | }
|
262 | 276 |
|
263 | 277 | void setError(String errResp) {
|
264 |
| - responseJSON = new JSONObject(errResp); // Parse error string to JSONObject |
| 278 | + try { |
| 279 | + responseJSON = new JSONObject(errResp); |
| 280 | + } catch (JSONException e) { |
| 281 | + // If errResp is not valid JSON, create a new JSONObject with the error message |
| 282 | + responseJSON = new JSONObject(); |
| 283 | + responseJSON.put(ERROR_MESSAGE, errResp); |
| 284 | + } |
265 | 285 | responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE));
|
266 | 286 | responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE));
|
267 | 287 | responseJSON.put(ERRORS, responseJSON.optString(ERRORS));
|
|
0 commit comments