diff options
author | Loic Blot <loic.blot@unix-experience.fr> | 2017-12-02 00:45:58 +0300 |
---|---|---|
committer | Loic Blot <loic.blot@unix-experience.fr> | 2017-12-02 00:45:58 +0300 |
commit | 580d4c158816b10f322fad4c8d2a889b5b327a77 (patch) | |
tree | ce35da2c10e7964a8a9e489364a2edb1c7c51be8 | |
parent | 141dfa40edd45898574901d6452ccaf7399267a4 (diff) |
Enhance connection error messages
Handle each exception in OCHttpClient::execute separately to help diagnose
-rw-r--r-- | src/main/java/fr/unix_experience/owncloud_sms/engine/OCHttpClient.java | 59 | ||||
-rw-r--r-- | src/main/res/values-en-rGB/strings.xml | 1 | ||||
-rw-r--r-- | src/main/res/values-en/strings.xml | 1 | ||||
-rw-r--r-- | src/main/res/values-fr/strings.xml | 1 | ||||
-rw-r--r-- | src/main/res/values/strings.xml | 1 |
5 files changed, 42 insertions, 21 deletions
diff --git a/src/main/java/fr/unix_experience/owncloud_sms/engine/OCHttpClient.java b/src/main/java/fr/unix_experience/owncloud_sms/engine/OCHttpClient.java index e255ceb..350024d 100644 --- a/src/main/java/fr/unix_experience/owncloud_sms/engine/OCHttpClient.java +++ b/src/main/java/fr/unix_experience/owncloud_sms/engine/OCHttpClient.java @@ -32,6 +32,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.net.ProtocolException; import java.net.URL; import java.nio.charset.Charset; @@ -152,36 +153,52 @@ public class OCHttpClient { HttpURLConnection urlConnection = null; try { urlConnection = (HttpURLConnection) url.openConnection(); - urlConnection.setRequestMethod(method); - urlConnection.setRequestProperty("User-Agent", _userAgent); - urlConnection.setInstanceFollowRedirects(true); - if (!"GET".equals(method)) { - urlConnection.setDoOutput(true); + } catch (IOException e) { + Log.e(OCHttpClient.TAG, "Failed to open connection to server: " + e); + throw new OCSyncException(R.string.err_sync_http_request_ioexception, OCSyncErrorType.IO); + } finally { + if (urlConnection != null) { + urlConnection.disconnect(); } - urlConnection.setRequestProperty("Content-Type", "application/json"); - urlConnection.setRequestProperty("Accept", "application/json"); - urlConnection.setRequestProperty("Transfer-Encoding", "chunked"); + } - String basicAuth = "Basic " + - Base64.encodeToString((_username + ":" + _password).getBytes(), Base64.NO_WRAP); - urlConnection.setRequestProperty("Authorization", basicAuth); - urlConnection.setChunkedStreamingMode(0); + if (urlConnection == null) { + Log.e(OCHttpClient.TAG, "Failed to open connection to server: null urlConnection"); + throw new OCSyncException(R.string.err_sync_http_request_ioexception, OCSyncErrorType.IO); + } - if (!"GET".equals(method)) { + try { + urlConnection.setRequestMethod(method); + } catch (ProtocolException e) { + Log.e(OCHttpClient.TAG, "Fatal error when setting request method: " + e); + throw new OCSyncException(R.string.err_sync_http_request_ioexception, OCSyncErrorType.IO); + } + urlConnection.setRequestProperty("User-Agent", _userAgent); + urlConnection.setInstanceFollowRedirects(true); + if (!"GET".equals(method)) { + urlConnection.setDoOutput(true); + } + urlConnection.setRequestProperty("Content-Type", "application/json"); + urlConnection.setRequestProperty("Accept", "application/json"); + urlConnection.setRequestProperty("Transfer-Encoding", "chunked"); + + String basicAuth = "Basic " + + Base64.encodeToString((_username + ":" + _password).getBytes(), Base64.NO_WRAP); + urlConnection.setRequestProperty("Authorization", basicAuth); + urlConnection.setChunkedStreamingMode(0); + + if (!"GET".equals(method)) { + try { OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); out.write(requestBody.getBytes(Charset.forName("UTF-8"))); out.close(); - } - - response = handleHTTPResponse(urlConnection, skipError); - } catch (IOException e) { - throw new OCSyncException(R.string.err_sync_http_request_ioexception, OCSyncErrorType.IO); - } finally { - if (urlConnection != null) { - urlConnection.disconnect(); + } catch (IOException e) { + Log.e(OCHttpClient.TAG, "Failed to open connection to server: " + e); + throw new OCSyncException(R.string.err_sync_http_write_failed, OCSyncErrorType.IO); } } + response = handleHTTPResponse(urlConnection, skipError); return response; } diff --git a/src/main/res/values-en-rGB/strings.xml b/src/main/res/values-en-rGB/strings.xml index 7bbcbbe..9bfbb89 100644 --- a/src/main/res/values-en-rGB/strings.xml +++ b/src/main/res/values-en-rGB/strings.xml @@ -183,4 +183,5 @@ <string name="no_confirm">No</string> <string name="pref_show_sync_notifications">Show sync notifications</string> <string name="sync_complete">Synchronisation complete</string> + <string name="err_sync_http_write_failed">Error #19: Failed to write HTTP stream when pushing data to server.</string> </resources> diff --git a/src/main/res/values-en/strings.xml b/src/main/res/values-en/strings.xml index 97e2561..229a81c 100644 --- a/src/main/res/values-en/strings.xml +++ b/src/main/res/values-en/strings.xml @@ -159,5 +159,6 @@ <string name="no_confirm">No</string> <string name="pref_show_sync_notifications">Show sync notifications</string> <string name="sync_complete">Synchronization complete</string> + <string name="err_sync_http_write_failed">Error #19: Failed to write HTTP stream when pushing data to server.</string> </resources> diff --git a/src/main/res/values-fr/strings.xml b/src/main/res/values-fr/strings.xml index 562530a..f2f3ee6 100644 --- a/src/main/res/values-fr/strings.xml +++ b/src/main/res/values-fr/strings.xml @@ -183,4 +183,5 @@ <string name="no_confirm">Non</string> <string name="pref_show_sync_notifications">Afficher les notifications de synchronisation</string> <string name="sync_complete">Synchronisation terminée</string> + <string name="err_sync_http_write_failed">Erreur #19: échec d\'écriture du flux HTTP lors de la poussée d\'informations vers le serveur.</string> </resources> diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 00b6c86..610caec 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -272,4 +272,5 @@ <string name="no_confirm">No</string> <string name="pref_show_sync_notifications">Show sync notifications</string> <string name="sync_complete">Synchronization complete</string> + <string name="err_sync_http_write_failed">Error #19: Failed to write HTTP stream when pushing data to server.</string> </resources> |