[API] Add error messages to exceptions (#71)

This commit is contained in:
Mikołaj Pich 2018-03-20 23:10:20 +01:00 committed by Rafał Borcz
parent 63b4ed42ca
commit 3dabb11473
15 changed files with 53 additions and 24 deletions

View File

@ -108,7 +108,11 @@ public class Client {
this.cookies.addItems(response.cookies()); 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 { public Document postPageByUrl(String url, String[][] params) throws IOException, VulcanException {
@ -165,16 +169,16 @@ public class Client {
} }
Document checkForErrors(Document doc) throws VulcanException { Document checkForErrors(Document doc) throws VulcanException {
if ("Przerwa techniczna".equals(doc.select("title").text())) { String title = doc.select("title").text();
throw new VulcanOfflineException(); if ("Przerwa techniczna".equals(title)) {
throw new VulcanOfflineException(title);
} }
if ("Zaloguj się".equals(doc.select(".loginButton").text())) { String singIn = doc.select(".loginButton").text();
throw new NotLoggedInErrorException(); if ("Zaloguj się".equals(singIn)) {
throw new NotLoggedInErrorException(singIn);
} }
lastSuccessRequest = new Date();
return doc; return doc;
} }
} }

View File

@ -1,4 +1,8 @@
package io.github.wulkanowy.api; package io.github.wulkanowy.api;
public class NotLoggedInErrorException extends VulcanException { public class NotLoggedInErrorException extends VulcanException {
public NotLoggedInErrorException(String message) {
super(message);
}
} }

View File

@ -48,7 +48,7 @@ public class StudentAndParent implements SnP {
Element studentTileLink = startPage.select(".panel.linkownia.pracownik.klient > a").first(); Element studentTileLink = startPage.select(".panel.linkownia.pracownik.klient > a").first();
if (null == studentTileLink) { if (null == studentTileLink) {
throw new NotLoggedInErrorException(); throw new NotLoggedInErrorException("You are probably not logged in. Force login");
} }
String snpPageUrl = studentTileLink.attr("href"); String snpPageUrl = studentTileLink.attr("href");
@ -62,7 +62,7 @@ public class StudentAndParent implements SnP {
String[] path = snpPageUrl.split(client.getHost())[1].split("/"); String[] path = snpPageUrl.split(client.getHost())[1].split("/");
if (5 != path.length) { if (5 != path.length) {
throw new NotLoggedInErrorException(); throw new NotLoggedInErrorException("You are probably not logged in");
} }
return path[2]; return path[2];

View File

@ -32,7 +32,7 @@ public class Vulcan {
public Client getClient() throws NotLoggedInErrorException { public Client getClient() throws NotLoggedInErrorException {
if (null == client) { if (null == client) {
throw new NotLoggedInErrorException(); throw new NotLoggedInErrorException("Use setCredentials() method first");
} }
return client; return client;

View File

@ -1,4 +1,8 @@
package io.github.wulkanowy.api; package io.github.wulkanowy.api;
public abstract class VulcanException extends Exception { public abstract class VulcanException extends Exception {
protected VulcanException(String message) {
super(message);
}
} }

View File

@ -1,4 +1,8 @@
package io.github.wulkanowy.api; package io.github.wulkanowy.api;
public class VulcanOfflineException extends VulcanException { public class VulcanOfflineException extends VulcanException {
VulcanOfflineException(String message) {
super(message);
}
} }

View File

@ -3,4 +3,8 @@ package io.github.wulkanowy.api.login;
import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.api.VulcanException;
public class AccountPermissionException extends VulcanException { public class AccountPermissionException extends VulcanException {
AccountPermissionException(String message) {
super(message);
}
} }

View File

@ -3,4 +3,8 @@ package io.github.wulkanowy.api.login;
import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.api.VulcanException;
public class BadCredentialsException extends VulcanException { public class BadCredentialsException extends VulcanException {
BadCredentialsException(String message) {
super(message);
}
} }

View File

@ -2,6 +2,7 @@ package io.github.wulkanowy.api.login;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser; import org.jsoup.parser.Parser;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
@ -42,8 +43,9 @@ public class Login {
{"Password", password} {"Password", password}
}); });
if (null != html.select(".ErrorMessage").first()) { Element errorMessage = html.select(".ErrorMessage").first();
throw new BadCredentialsException(); if (null != errorMessage) {
throw new BadCredentialsException(errorMessage.text());
} }
return html.select("input[name=wresult]").attr("value"); return html.select("input[name=wresult]").attr("value");
@ -59,11 +61,11 @@ public class Login {
}).select("title").text(); }).select("title").text();
if ("Logowanie".equals(title)) { if ("Logowanie".equals(title)) {
throw new AccountPermissionException(); throw new AccountPermissionException("No account access. Try another symbol");
} }
if (!"Uonet+".equals(title)) { if (!"Uonet+".equals(title)) {
throw new LoginErrorException(); throw new LoginErrorException("Could not log in, unknown error");
} }
return this.symbol; return this.symbol;

View File

@ -2,5 +2,9 @@ package io.github.wulkanowy.api.login;
import io.github.wulkanowy.api.NotLoggedInErrorException; import io.github.wulkanowy.api.NotLoggedInErrorException;
public class LoginErrorException extends NotLoggedInErrorException { class LoginErrorException extends NotLoggedInErrorException {
LoginErrorException(String message) {
super(message);
}
} }

View File

@ -3,4 +3,8 @@ package io.github.wulkanowy.api.messages;
import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.api.VulcanException;
class BadRequestException extends VulcanException { class BadRequestException extends VulcanException {
BadRequestException(String message) {
super(message);
}
} }

View File

@ -59,10 +59,10 @@ public class Messages {
messages = new Gson().fromJson(res, MessagesContainer.class).data; messages = new Gson().fromJson(res, MessagesContainer.class).data;
} catch (JsonParseException e) { } catch (JsonParseException e) {
if (res.contains(ERROR_TITLE)) { 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; return messages;
@ -80,10 +80,10 @@ public class Messages {
message = new Gson().fromJson(res, MessageContainer.class).data; message = new Gson().fromJson(res, MessageContainer.class).data;
} catch (JsonParseException e) { } catch (JsonParseException e) {
if (res.contains(ERROR_TITLE)) { 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; return message;

View File

@ -12,7 +12,6 @@ import javax.inject.Singleton;
import io.github.wulkanowy.R; import io.github.wulkanowy.R;
import io.github.wulkanowy.api.NotLoggedInErrorException; 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.data.db.dao.entities.AttendanceLesson;
import io.github.wulkanowy.di.annotations.ApplicationContext; import io.github.wulkanowy.di.annotations.ApplicationContext;
import io.github.wulkanowy.utils.AppConstant; import io.github.wulkanowy.utils.AppConstant;
@ -51,8 +50,6 @@ public class AppResources implements ResourcesContract {
return resources.getString(R.string.generic_timeout_error); return resources.getString(R.string.generic_timeout_error);
} else if (exception instanceof NotLoggedInErrorException || exception instanceof IOException) { } else if (exception instanceof NotLoggedInErrorException || exception instanceof IOException) {
return resources.getString(R.string.login_denied_text); return resources.getString(R.string.login_denied_text);
} else if (exception instanceof VulcanOfflineException) {
return resources.getString(R.string.error_host_offline);
} else { } else {
return exception.getMessage(); return exception.getMessage();
} }

View File

@ -39,7 +39,6 @@
<string name="fragment_no_grades">Brak ocen</string> <string name="fragment_no_grades">Brak ocen</string>
<string name="noInternet_text">Brak połączenia z internetem</string> <string name="noInternet_text">Brak połączenia z internetem</string>
<string name="error_host_offline">Przerwa techniczna. Spróbuj ponownie później</string>
<string name="encrypt_failed_text">Szyfrowanie nie powiodło się. Automatyczne logowanie zostało wyłączone</string> <string name="encrypt_failed_text">Szyfrowanie nie powiodło się. Automatyczne logowanie zostało wyłączone</string>
<string name="version_text">Wersja %1$s</string> <string name="version_text">Wersja %1$s</string>
<string name="refresh_error_text">"Podczas odświeżania zawartości wystąpił błąd. "</string> <string name="refresh_error_text">"Podczas odświeżania zawartości wystąpił błąd. "</string>

View File

@ -73,7 +73,6 @@
<string name="info_average_grades">Average: %1$.2f</string> <string name="info_average_grades">Average: %1$.2f</string>
<string name="info_no_average">No average</string> <string name="info_no_average">No average</string>
<string name="info_free_week">No lesson in this week</string> <string name="info_free_week">No lesson in this week</string>
<string name="error_host_offline">Technical break</string>
<string name="timetable_subitem_room">Room %s</string> <string name="timetable_subitem_room">Room %s</string>