From 87459563c4f3cbd7a66d56b35a2c9cc4d8c27ea9 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Wed, 27 Mar 2024 13:36:34 -0500 Subject: [PATCH] feat: update error handling to log more consistently --- pom.xml | 2 +- .../com/bigboxer23/switch_bot/SwitchBotApi.java | 13 +++++++------ .../bigboxer23/switch_bot/SwitchBotDeviceApi.java | 3 +-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index ca15e63..7c3fb14 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.bigboxer23 switchbotapi-java - 1.1.4 + 1.1.5 switchbotapi-java https://github.com/bigboxer23/switchbotapi-java diff --git a/src/main/java/com/bigboxer23/switch_bot/SwitchBotApi.java b/src/main/java/com/bigboxer23/switch_bot/SwitchBotApi.java index b9375cc..e6c8235 100644 --- a/src/main/java/com/bigboxer23/switch_bot/SwitchBotApi.java +++ b/src/main/java/com/bigboxer23/switch_bot/SwitchBotApi.java @@ -11,6 +11,7 @@ import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import lombok.Getter; +import okhttp3.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -78,17 +79,17 @@ protected RequestBuilderCallback addAuth() { * @param apiResponse the API response * @return true if error occurs */ - protected boolean checkForError(IApiResponse apiResponse) { - return Optional.ofNullable(apiResponse) - .map(response -> { - if (response.getStatusCode() != 100) { - logger.error("error code: " + response.getStatusCode() + " : " + response.getMessage()); + protected boolean checkForError(Response response, Optional apiResponse) { + return apiResponse + .map(api -> { + if (api.getStatusCode() != 100) { + logger.error("error code: " + api.getStatusCode() + " : " + api.getMessage()); return false; } return true; }) .orElseGet(() -> { - logger.error("null api response"); + logger.error("Error calling switchbot api: " + response.code() + " " + response.message()); return false; }); } diff --git a/src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java b/src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java index b70b301..790840b 100644 --- a/src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java +++ b/src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java @@ -7,7 +7,6 @@ import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Optional; - import okhttp3.RequestBody; import okhttp3.Response; import org.slf4j.Logger; @@ -78,7 +77,7 @@ public void sendDeviceControlCommands(String deviceId, DeviceCommand command) th */ private T parseResponse(Response response, Class clazz) throws IOException { Optional apiResponse = OkHttpUtil.getBody(response, clazz); - if (apiResponse.isEmpty() || !provider.checkForError(apiResponse.get())) { + if (!provider.checkForError(response, (Optional) apiResponse)) { throw new IOException(response.code() + " " + response.message()); } return apiResponse.get();