From 5d2dbeee95bed9841fce20562fdf6831c9b93ab9 Mon Sep 17 00:00:00 2001 From: suuft Date: Sun, 22 Jan 2023 20:08:16 +0300 Subject: [PATCH] feat: exception on troubles --- README.md | 4 ++-- build.gradle | 2 +- .../net/suuft/libretranslate/Translator.java | 5 +++-- .../BadTranslatorResponseException.java | 16 ++++++++++++++++ 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 src/main/java/net/suuft/libretranslate/exception/BadTranslatorResponseException.java diff --git a/README.md b/README.md index b22c5fd..e5c86e0 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ repositories { dependencies { // other depends - implementation 'net.clojars.suuft:libretranslate-java:1.0.4' + implementation 'net.clojars.suuft:libretranslate-java:1.0.5' } ``` @@ -38,7 +38,7 @@ Depend: net.clojars.suuft libretranslate-java - 1.0.4 + 1.0.5 ``` ### `Usage:` diff --git a/build.gradle b/build.gradle index b676b48..d6af772 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'net.clojars.suuft' -version '1.0.3' +version '1.0.5' tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } diff --git a/src/main/java/net/suuft/libretranslate/Translator.java b/src/main/java/net/suuft/libretranslate/Translator.java index 69f09a0..b975ecb 100644 --- a/src/main/java/net/suuft/libretranslate/Translator.java +++ b/src/main/java/net/suuft/libretranslate/Translator.java @@ -3,6 +3,7 @@ import lombok.NonNull; import lombok.Setter; import lombok.experimental.UtilityClass; +import net.suuft.libretranslate.exception.BadTranslatorResponseException; import net.suuft.libretranslate.type.TranslateResponse; import net.suuft.libretranslate.util.JsonUtil; import java.io.InputStream; @@ -35,7 +36,7 @@ public String translate(@NonNull String from, @NonNull String to, @NonNull Strin writer.close(); httpConn.getOutputStream().close(); - if (!(httpConn.getResponseCode() / 100 == 2)) return "Falled translate!"; + if (!(httpConn.getResponseCode() / 100 == 2)) throw new BadTranslatorResponseException(httpConn.getResponseCode(), urlApi); InputStream responseStream = httpConn.getInputStream(); Scanner s = new Scanner(responseStream).useDelimiter("\\A"); @@ -44,7 +45,7 @@ public String translate(@NonNull String from, @NonNull String to, @NonNull Strin return JsonUtil.from(response, TranslateResponse.class).getTranslatedText(); } catch (Exception e) { e.printStackTrace(); - return "Falled translate!"; + throw new RuntimeException(e); } } diff --git a/src/main/java/net/suuft/libretranslate/exception/BadTranslatorResponseException.java b/src/main/java/net/suuft/libretranslate/exception/BadTranslatorResponseException.java new file mode 100644 index 0000000..0d87eff --- /dev/null +++ b/src/main/java/net/suuft/libretranslate/exception/BadTranslatorResponseException.java @@ -0,0 +1,16 @@ +package net.suuft.libretranslate.exception; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.experimental.FieldDefaults; + +@Getter +@AllArgsConstructor +@FieldDefaults(level = AccessLevel.PRIVATE) +public class BadTranslatorResponseException extends Exception { + + int code; + String host; + +}