From aa32c60a0aca4ad35106bb98a214fc724ab090cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 31 Jul 2014 18:07:11 +0200 Subject: [PATCH] Even better Exception handling with keyserver queries --- .../keychain/keyimport/HkpKeyserver.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java index 7f7d10319..f35ef45a2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java @@ -217,7 +217,7 @@ public class HkpKeyserver extends Keyserver { throw new HttpError(response, data); } } catch (IOException e) { - throw new QueryFailedException("querying server(s) for '" + mHost + "' failed"); + throw new QueryFailedException("Keyserver '" + mHost + "' is unavailable. Check your Internet connection!"); } } @@ -242,24 +242,26 @@ public class HkpKeyserver extends Keyserver { try { data = query(request); } catch (HttpError e) { - if (e.getCode() == 404) { - throw new QueryFailedException("keyserver '" + mHost + "' not found. Error 404"); - } else if (e.getData() != null) { + if (e.getData() != null) { Log.d(Constants.TAG, "returned error data: " + e.getData().toLowerCase(Locale.US)); if (e.getData().toLowerCase(Locale.US).contains("no keys found")) { + // NOTE: This is also a 404 error for some keyservers! return results; } else if (e.getData().toLowerCase(Locale.US).contains("too many")) { throw new TooManyResponsesException(); } else if (e.getData().toLowerCase(Locale.US).contains("insufficient")) { throw new QueryTooShortException(); + } else if (e.getCode() == 404) { + // NOTE: handle this 404 at last, maybe it was a "no keys found" error + throw new QueryFailedException("Keyserver '" + mHost + "' not found. Error 404"); + } else { + // NOTE: some keyserver do not provide a more detailed error response + throw new QueryFailedException("Either no keys or too many have been found. Please improve your query!"); } - - // NOTE: some keyserver do not provide a more detailed error response - throw new QueryFailedException("Either no keys or too many have been found. Please improve your query!"); } - throw new QueryFailedException("querying server(s) for '" + mHost + "' failed"); + throw new QueryFailedException("Querying server(s) for '" + mHost + "' failed."); } final Matcher matcher = PUB_KEY_LINE.matcher(data);