From 3dabb1147354c9f508826cf2fa5d7139c15c5e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Tue, 20 Mar 2018 23:10:20 +0100 Subject: [PATCH] [API] Add error messages to exceptions (#71) --- .../java/io/github/wulkanowy/api/Client.java | 18 +++++++++++------- .../api/NotLoggedInErrorException.java | 4 ++++ .../github/wulkanowy/api/StudentAndParent.java | 4 ++-- .../java/io/github/wulkanowy/api/Vulcan.java | 2 +- .../github/wulkanowy/api/VulcanException.java | 4 ++++ .../wulkanowy/api/VulcanOfflineException.java | 4 ++++ .../api/login/AccountPermissionException.java | 4 ++++ .../api/login/BadCredentialsException.java | 4 ++++ .../io/github/wulkanowy/api/login/Login.java | 10 ++++++---- .../api/login/LoginErrorException.java | 6 +++++- .../api/messages/BadRequestException.java | 4 ++++ .../wulkanowy/api/messages/Messages.java | 8 ++++---- .../data/db/resources/AppResources.java | 3 --- app/src/main/res/values-pl/strings.xml | 1 - app/src/main/res/values/strings.xml | 1 - 15 files changed, 53 insertions(+), 24 deletions(-) diff --git a/api/src/main/java/io/github/wulkanowy/api/Client.java b/api/src/main/java/io/github/wulkanowy/api/Client.java index 16b7982c..c3abb2ee 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Client.java +++ b/api/src/main/java/io/github/wulkanowy/api/Client.java @@ -108,7 +108,11 @@ public class Client { this.cookies.addItems(response.cookies()); - return checkForErrors(response.parse()); + Document doc = checkForErrors(response.parse()); + + lastSuccessRequest = new Date(); + + return doc; } public Document postPageByUrl(String url, String[][] params) throws IOException, VulcanException { @@ -165,16 +169,16 @@ public class Client { } Document checkForErrors(Document doc) throws VulcanException { - if ("Przerwa techniczna".equals(doc.select("title").text())) { - throw new VulcanOfflineException(); + String title = doc.select("title").text(); + if ("Przerwa techniczna".equals(title)) { + throw new VulcanOfflineException(title); } - if ("Zaloguj się".equals(doc.select(".loginButton").text())) { - throw new NotLoggedInErrorException(); + String singIn = doc.select(".loginButton").text(); + if ("Zaloguj się".equals(singIn)) { + throw new NotLoggedInErrorException(singIn); } - lastSuccessRequest = new Date(); - return doc; } } diff --git a/api/src/main/java/io/github/wulkanowy/api/NotLoggedInErrorException.java b/api/src/main/java/io/github/wulkanowy/api/NotLoggedInErrorException.java index 86372266..179fc8cc 100644 --- a/api/src/main/java/io/github/wulkanowy/api/NotLoggedInErrorException.java +++ b/api/src/main/java/io/github/wulkanowy/api/NotLoggedInErrorException.java @@ -1,4 +1,8 @@ package io.github.wulkanowy.api; public class NotLoggedInErrorException extends VulcanException { + + public NotLoggedInErrorException(String message) { + super(message); + } } diff --git a/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java b/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java index c014e96c..8f67da39 100644 --- a/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java +++ b/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java @@ -48,7 +48,7 @@ public class StudentAndParent implements SnP { Element studentTileLink = startPage.select(".panel.linkownia.pracownik.klient > a").first(); if (null == studentTileLink) { - throw new NotLoggedInErrorException(); + throw new NotLoggedInErrorException("You are probably not logged in. Force login"); } String snpPageUrl = studentTileLink.attr("href"); @@ -62,7 +62,7 @@ public class StudentAndParent implements SnP { String[] path = snpPageUrl.split(client.getHost())[1].split("/"); if (5 != path.length) { - throw new NotLoggedInErrorException(); + throw new NotLoggedInErrorException("You are probably not logged in"); } return path[2]; diff --git a/api/src/main/java/io/github/wulkanowy/api/Vulcan.java b/api/src/main/java/io/github/wulkanowy/api/Vulcan.java index c16ef088..75b939dd 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Vulcan.java +++ b/api/src/main/java/io/github/wulkanowy/api/Vulcan.java @@ -32,7 +32,7 @@ public class Vulcan { public Client getClient() throws NotLoggedInErrorException { if (null == client) { - throw new NotLoggedInErrorException(); + throw new NotLoggedInErrorException("Use setCredentials() method first"); } return client; diff --git a/api/src/main/java/io/github/wulkanowy/api/VulcanException.java b/api/src/main/java/io/github/wulkanowy/api/VulcanException.java index 0e7ed243..723cab91 100644 --- a/api/src/main/java/io/github/wulkanowy/api/VulcanException.java +++ b/api/src/main/java/io/github/wulkanowy/api/VulcanException.java @@ -1,4 +1,8 @@ package io.github.wulkanowy.api; public abstract class VulcanException extends Exception { + + protected VulcanException(String message) { + super(message); + } } diff --git a/api/src/main/java/io/github/wulkanowy/api/VulcanOfflineException.java b/api/src/main/java/io/github/wulkanowy/api/VulcanOfflineException.java index 497fba94..24ab48e6 100644 --- a/api/src/main/java/io/github/wulkanowy/api/VulcanOfflineException.java +++ b/api/src/main/java/io/github/wulkanowy/api/VulcanOfflineException.java @@ -1,4 +1,8 @@ package io.github.wulkanowy.api; public class VulcanOfflineException extends VulcanException { + + VulcanOfflineException(String message) { + super(message); + } } diff --git a/api/src/main/java/io/github/wulkanowy/api/login/AccountPermissionException.java b/api/src/main/java/io/github/wulkanowy/api/login/AccountPermissionException.java index 99feb86f..de3901ff 100644 --- a/api/src/main/java/io/github/wulkanowy/api/login/AccountPermissionException.java +++ b/api/src/main/java/io/github/wulkanowy/api/login/AccountPermissionException.java @@ -3,4 +3,8 @@ package io.github.wulkanowy.api.login; import io.github.wulkanowy.api.VulcanException; public class AccountPermissionException extends VulcanException { + + AccountPermissionException(String message) { + super(message); + } } diff --git a/api/src/main/java/io/github/wulkanowy/api/login/BadCredentialsException.java b/api/src/main/java/io/github/wulkanowy/api/login/BadCredentialsException.java index 1ac37f9c..13efd4a1 100644 --- a/api/src/main/java/io/github/wulkanowy/api/login/BadCredentialsException.java +++ b/api/src/main/java/io/github/wulkanowy/api/login/BadCredentialsException.java @@ -3,4 +3,8 @@ package io.github.wulkanowy.api.login; import io.github.wulkanowy.api.VulcanException; public class BadCredentialsException extends VulcanException { + + BadCredentialsException(String message) { + super(message); + } } diff --git a/api/src/main/java/io/github/wulkanowy/api/login/Login.java b/api/src/main/java/io/github/wulkanowy/api/login/Login.java index 7f88037d..e7aecec0 100644 --- a/api/src/main/java/io/github/wulkanowy/api/login/Login.java +++ b/api/src/main/java/io/github/wulkanowy/api/login/Login.java @@ -2,6 +2,7 @@ package io.github.wulkanowy.api.login; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; import org.jsoup.parser.Parser; import org.jsoup.select.Elements; @@ -42,8 +43,9 @@ public class Login { {"Password", password} }); - if (null != html.select(".ErrorMessage").first()) { - throw new BadCredentialsException(); + Element errorMessage = html.select(".ErrorMessage").first(); + if (null != errorMessage) { + throw new BadCredentialsException(errorMessage.text()); } return html.select("input[name=wresult]").attr("value"); @@ -59,11 +61,11 @@ public class Login { }).select("title").text(); if ("Logowanie".equals(title)) { - throw new AccountPermissionException(); + throw new AccountPermissionException("No account access. Try another symbol"); } if (!"Uonet+".equals(title)) { - throw new LoginErrorException(); + throw new LoginErrorException("Could not log in, unknown error"); } return this.symbol; diff --git a/api/src/main/java/io/github/wulkanowy/api/login/LoginErrorException.java b/api/src/main/java/io/github/wulkanowy/api/login/LoginErrorException.java index e264dc67..be7439df 100644 --- a/api/src/main/java/io/github/wulkanowy/api/login/LoginErrorException.java +++ b/api/src/main/java/io/github/wulkanowy/api/login/LoginErrorException.java @@ -2,5 +2,9 @@ package io.github.wulkanowy.api.login; import io.github.wulkanowy.api.NotLoggedInErrorException; -public class LoginErrorException extends NotLoggedInErrorException { +class LoginErrorException extends NotLoggedInErrorException { + + LoginErrorException(String message) { + super(message); + } } diff --git a/api/src/main/java/io/github/wulkanowy/api/messages/BadRequestException.java b/api/src/main/java/io/github/wulkanowy/api/messages/BadRequestException.java index ed407b4d..14dca67b 100644 --- a/api/src/main/java/io/github/wulkanowy/api/messages/BadRequestException.java +++ b/api/src/main/java/io/github/wulkanowy/api/messages/BadRequestException.java @@ -3,4 +3,8 @@ package io.github.wulkanowy.api.messages; import io.github.wulkanowy.api.VulcanException; class BadRequestException extends VulcanException { + + BadRequestException(String message) { + super(message); + } } diff --git a/api/src/main/java/io/github/wulkanowy/api/messages/Messages.java b/api/src/main/java/io/github/wulkanowy/api/messages/Messages.java index eb5f8bba..ec2a3fba 100644 --- a/api/src/main/java/io/github/wulkanowy/api/messages/Messages.java +++ b/api/src/main/java/io/github/wulkanowy/api/messages/Messages.java @@ -59,10 +59,10 @@ public class Messages { messages = new Gson().fromJson(res, MessagesContainer.class).data; } catch (JsonParseException e) { if (res.contains(ERROR_TITLE)) { - throw new BadRequestException(); + throw new BadRequestException(ERROR_TITLE); } - throw new NotLoggedInErrorException(); + throw new NotLoggedInErrorException("You are probably not logged in"); } return messages; @@ -80,10 +80,10 @@ public class Messages { message = new Gson().fromJson(res, MessageContainer.class).data; } catch (JsonParseException e) { if (res.contains(ERROR_TITLE)) { - throw new BadRequestException(); + throw new BadRequestException(ERROR_TITLE); } - throw new NotLoggedInErrorException(); + throw new NotLoggedInErrorException("You are probably not logged in. Force login"); } return message; diff --git a/app/src/main/java/io/github/wulkanowy/data/db/resources/AppResources.java b/app/src/main/java/io/github/wulkanowy/data/db/resources/AppResources.java index 349ac177..c35f44c7 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/resources/AppResources.java +++ b/app/src/main/java/io/github/wulkanowy/data/db/resources/AppResources.java @@ -12,7 +12,6 @@ import javax.inject.Singleton; import io.github.wulkanowy.R; import io.github.wulkanowy.api.NotLoggedInErrorException; -import io.github.wulkanowy.api.VulcanOfflineException; import io.github.wulkanowy.data.db.dao.entities.AttendanceLesson; import io.github.wulkanowy.di.annotations.ApplicationContext; import io.github.wulkanowy.utils.AppConstant; @@ -51,8 +50,6 @@ public class AppResources implements ResourcesContract { return resources.getString(R.string.generic_timeout_error); } else if (exception instanceof NotLoggedInErrorException || exception instanceof IOException) { return resources.getString(R.string.login_denied_text); - } else if (exception instanceof VulcanOfflineException) { - return resources.getString(R.string.error_host_offline); } else { return exception.getMessage(); } diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 9642a53d..244f39e8 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -39,7 +39,6 @@ Brak ocen Brak połączenia z internetem - Przerwa techniczna. Spróbuj ponownie później Szyfrowanie nie powiodło się. Automatyczne logowanie zostało wyłączone Wersja %1$s "Podczas odświeżania zawartości wystąpił błąd. " diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 315fee4f..b4b6c0c0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -73,7 +73,6 @@ Average: %1$.2f No average No lesson in this week - Technical break Room %s