1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2025-06-07 16:06:18 +00:00

Enhance connection error messages

Handle each exception in OCHttpClient::execute separately to help diagnose
This commit is contained in:
Loic Blot 2017-12-01 22:45:58 +01:00
parent 141dfa40ed
commit 580d4c1588
No known key found for this signature in database
GPG Key ID: EFAA458E8C153987
5 changed files with 43 additions and 22 deletions

View File

@ -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,7 +153,26 @@ public class OCHttpClient {
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
} 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();
}
}
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);
}
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)) {
@ -168,20 +188,17 @@ public class OCHttpClient {
urlConnection.setChunkedStreamingMode(0);
if (!"GET".equals(method)) {
try {
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
out.write(requestBody.getBytes(Charset.forName("UTF-8")));
out.close();
} 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);
} catch (IOException e) {
throw new OCSyncException(R.string.err_sync_http_request_ioexception, OCSyncErrorType.IO);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return response;
}

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>