Skip to content

Commit

Permalink
feat: exception on troubles
Browse files Browse the repository at this point in the history
  • Loading branch information
suuft committed Jan 22, 2023
1 parent 5c2b688 commit 5d2dbee
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand All @@ -38,7 +38,7 @@ Depend:
<dependency>
<groupId>net.clojars.suuft</groupId>
<artifactId>libretranslate-java</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
</dependency>
```
### `Usage:`
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'net.clojars.suuft'
version '1.0.3'
version '1.0.5'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/suuft/libretranslate/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

}

0 comments on commit 5d2dbee

Please sign in to comment.