From 9a705e911485af200937ed4fbbd216f1ecefdea2 Mon Sep 17 00:00:00 2001 From: Jakob Heher Date: Wed, 16 Nov 2022 12:04:34 +0100 Subject: explicit error messages for http non-200 cf. #133 --- .../src/main/java/at/asit/pdfover/commons/Messages.java | 14 ++++++++++---- .../at/asit/pdfover/gui/bku/MobileBKUConnector.java | 17 +++++++++++++++-- .../pdfover/gui/workflow/states/MobileBKUState.java | 2 +- .../resources/at/asit/pdfover/gui/messages.properties | 2 ++ .../at/asit/pdfover/gui/messages_de.properties | 2 ++ 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/pdf-over-commons/src/main/java/at/asit/pdfover/commons/Messages.java b/pdf-over-commons/src/main/java/at/asit/pdfover/commons/Messages.java index 04c8583a..d8f204ea 100644 --- a/pdf-over-commons/src/main/java/at/asit/pdfover/commons/Messages.java +++ b/pdf-over-commons/src/main/java/at/asit/pdfover/commons/Messages.java @@ -27,6 +27,8 @@ import javax.annotation.Nonnull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static at.asit.pdfover.commons.Constants.ISNOTNULL; + /** * Localizes string messages for PDFOver GUI @@ -57,7 +59,7 @@ public class Messages { if (l.equals(ld) || l.getLanguage().equals(ld.getLanguage())) return l; } - return Constants.ISNOTNULL(Constants.SUPPORTED_LOCALES[0]); + return ISNOTNULL(Constants.SUPPORTED_LOCALES[0]); } /** @@ -94,7 +96,7 @@ public class Messages { * @param key * @return the localized message */ - public static String getString(String key) { + public static @Nonnull String getString(String key) { return getString(key, currentLocale); } @@ -104,9 +106,9 @@ public class Messages { * @param locale the locale to use * @return the localized message */ - public static String getString(String key, Locale locale) { + public static @Nonnull String getString(String key, Locale locale) { try { - String value = getBundle(locale).getString(key); + String value = ISNOTNULL(getBundle(locale).getString(key)); /* DIRTY HACK: this recognizes java 8 ("1.8") and older; these versions read .properties files as ISO-8859-1 instead of UTF-8 */ if (System.getProperty("java.version").startsWith("1.")) @@ -118,4 +120,8 @@ public class Messages { return '!' + key + '!'; } } + + public static @Nonnull String formatString(String key, Object... values) { + return ISNOTNULL(String.format(getString(key), values)); + } } diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/MobileBKUConnector.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/MobileBKUConnector.java index 58762b03..29bf880f 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/MobileBKUConnector.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/MobileBKUConnector.java @@ -40,6 +40,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.asit.pdfover.commons.Constants; +import at.asit.pdfover.commons.Messages; import at.asit.pdfover.gui.bku.mobile.ATrustParser; import at.asit.pdfover.gui.workflow.states.MobileBKUState; import at.asit.pdfover.gui.workflow.states.MobileBKUState.UsernameAndPassword; @@ -60,6 +61,12 @@ public class MobileBKUConnector implements BkuSlConnector { state.storeRememberedCredentialsTo(this.credentials); } + private class UserDisplayedError extends Exception { + private final @Nonnull String msg; + @Override public @Nonnull String getMessage() { return this.msg; } + private UserDisplayedError(@Nonnull String s) { this.msg = s; } + } + public @Nonnull UsernameAndPassword credentials = new UsernameAndPassword(); /** @@ -75,6 +82,9 @@ public class MobileBKUConnector implements BkuSlConnector { currentRequest = presentResponseToUserAndReturnNextRequest(ISNOTNULL(response.html)); log.debug("Returning security layer response:\n{}", response.slResponse); return response.slResponse; + } catch (UserDisplayedError e) { + state.showUnrecoverableError(e.getMessage()); + throw new IllegalStateException("unreachable", e); /* showUnrecoverableError always throws */ } catch (UserCancelledException e) { throw e; } catch (Exception e) { @@ -93,7 +103,7 @@ public class MobileBKUConnector implements BkuSlConnector { * @throws URISyntaxException * @throws InterruptedException */ - private @Nonnull ATrustParser.Result sendHTTPRequest(CloseableHttpClient httpClient, ClassicHttpRequest request) throws IOException, ProtocolException, URISyntaxException { + private @Nonnull ATrustParser.Result sendHTTPRequest(CloseableHttpClient httpClient, ClassicHttpRequest request) throws IOException, ProtocolException, URISyntaxException, UserDisplayedError { long now = System.nanoTime(); if ((lastHTTPRequestTime != null) && ((now - lastHTTPRequestTime) < 2e+9)) { /* less than 2s since last request */ ++loopHTTPRequestCounter; @@ -115,7 +125,10 @@ public class MobileBKUConnector implements BkuSlConnector { } if (httpStatus != HttpStatus.SC_OK) { - throw new IOException("Got HTTP status " + httpStatus + " " + Optional.ofNullable(response.getReasonPhrase()).orElse("(null)")); + switch (httpStatus) { + case HttpStatus.SC_REQUEST_TOO_LONG: throw new UserDisplayedError(Messages.getString("atrusterror.http_413")); + default: throw new UserDisplayedError(Messages.formatString("atrusterror.http_generic", httpStatus, Optional.ofNullable(response.getReasonPhrase()).orElse("(null)"))); + } } Header refreshHeader = response.getHeader("refresh"); diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/MobileBKUState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/MobileBKUState.java index e1ecf39c..cec7acd5 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/MobileBKUState.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/MobileBKUState.java @@ -215,7 +215,7 @@ public class MobileBKUState extends State { } /** - * Show an error message to the user with only an "ok" option + * Show an error message to the user with only an "ok" option; * throws UserCancelledException afterwards */ public void showUnrecoverableError(final @Nonnull String errorMessage) throws UserCancelledException { diff --git a/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages.properties b/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages.properties index 031b32e6..a1027e3e 100644 --- a/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages.properties +++ b/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages.properties @@ -273,3 +273,5 @@ config.fido2.UnsupportedPlatform=FIDO2 authentication is currently unsupported o config.fido2.InfoURL=https://www.oesterreich.gv.at/id-austria/haeufige-fragen/allgemeines-zu-id-austria#fido config.fido2.Group=FIDO2 settings config.fido2.ByDefault=Select FIDO2 authentication by default +atrusterror.http_413=a-trust.at rejected request:\nProvided document exceeds size limit. +atrusterror.http_generic=a-trust.at rejected request:\nHTTP %d %s diff --git a/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages_de.properties b/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages_de.properties index 58c10267..82a49c25 100644 --- a/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages_de.properties +++ b/pdf-over-gui/src/main/resources/at/asit/pdfover/gui/messages_de.properties @@ -265,3 +265,5 @@ config.UpdateStatus.CHECKING=Versions-Infos werden abgefragt... config.UpdateStatus.OUTDATED=Neue Version verfügbar. Herunterladen? config.UpdateStatus.UP_TO_DATE=Ihre Version ist aktuell. Nochmals prüfen? config.UpdateStatus.FAILED=Versions-Check fehlgeschlagen. Nochmal versuchen? +atrusterror.http_413=a-trust.at lehnt Anfrage ab:\nDateigröße des PDF-Dokuments überschreitet Grenzwert. +atrusterror.http_generic=a-trust.at lehnt Anfrage ab:\nHTTP %d %s -- cgit v1.2.3