Skip to content

Commit

Permalink
[ecovacs] Handle invalid JSON responses properly
Browse files Browse the repository at this point in the history
The API has differing responses depending on device type. If our
understanding of the JSON format differs from that of the API, make sure
to properly set the thing OFFLINE and to log a meaningful message.

Related to openhab#16187

Signed-off-by: Danny Baumann <dannybaumann@web.de>
  • Loading branch information
maniac103 committed Feb 27, 2024
1 parent b6366ab commit 8b04bf6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public EcovacsApiException(String reason) {
this(reason, false);
}

public EcovacsApiException(String reason, Throwable cause) {
super(reason, cause);
isAuthFailure = false;
}

public EcovacsApiException(String reason, boolean isAuthFailure) {
super(reason);
this.isAuthFailure = isAuthFailure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,18 @@ private <T> T handleResponseWrapper(@Nullable ResponseWrapper<T> response) throw
}

private <T> T handleResponse(ContentResponse response, Class<T> clazz) throws EcovacsApiException {
@Nullable
T respObject = gson.fromJson(response.getContentAsString(), clazz);
if (respObject == null) {
// should not happen in practice
throw new EcovacsApiException("No response received");
try {
@Nullable
T respObject = gson.fromJson(response.getContentAsString(), clazz);
if (respObject == null) {
// should not happen in practice
throw new EcovacsApiException("No response received");
}
return respObject;
} catch (IllegalStateException e) {
throw new EcovacsApiException("Failed to parse response '" + response.getContentAsString()
+ "' as data class " + clazz.getSimpleName(), e);
}
return respObject;
}

private Request createAuthRequest(String url, String clientKey, String clientSecret,
Expand Down

0 comments on commit 8b04bf6

Please sign in to comment.