Skip to content

Commit

Permalink
fix: handle case where HTTP response is not valid JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-jones-dev committed Oct 13, 2022
1 parent ed420f8 commit 0e89acf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [Unreleased]
### Fixed
* Handle case where HTTP response is not valid JSON.


## [0.2.0] - 2022-09-26
### Added
* Add new `Formality` options: `PreferLess` and `PreferMore`.
Expand Down Expand Up @@ -40,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Initial version.


[Unreleased]: https://github.com/DeepLcom/deepl-java/compare/v0.2.0...HEAD
[0.2.0]: https://github.com/DeepLcom/deepl-java/compare/v0.1.3...v0.2.0
[0.1.3]: https://github.com/DeepLcom/deepl-java/compare/v0.1.2...v0.1.3
[0.1.2]: https://github.com/DeepLcom/deepl-java/compare/v0.1.1...v0.1.2
Expand Down
13 changes: 10 additions & 3 deletions deepl-java/src/main/java/com/deepl/api/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.deepl.api.http.HttpResponseStream;
import com.deepl.api.parsing.Parser;
import com.deepl.api.utils.*;
import com.google.gson.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.util.*;
Expand Down Expand Up @@ -766,10 +767,16 @@ private void checkResponse(HttpResponse response, boolean inDocumentDownload)
return;
}

String messageSuffix = jsonParser.parseErrorMessage(response.getBody());
if (!messageSuffix.isEmpty()) {
messageSuffix = ", " + messageSuffix;
String messageSuffix = "";
String body = response.getBody();
if (body != null && !body.isEmpty()) {
try {
messageSuffix = ", error message: " + jsonParser.parseErrorMessage(body);
} catch (JsonSyntaxException ignored) {
messageSuffix = ", response: " + body;
}
}

switch (response.getCode()) {
case HttpURLConnection.HTTP_BAD_REQUEST:
throw new DeepLException("Bad request" + messageSuffix);
Expand Down

0 comments on commit 0e89acf

Please sign in to comment.