Skip to content

Commit 0666c4a

Browse files
authored
feat: update error handling to log more consistently (#39)
1 parent d223477 commit 0666c4a

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.bigboxer23</groupId>
66
<artifactId>switchbotapi-java</artifactId>
7-
<version>1.1.4</version>
7+
<version>1.1.5</version>
88

99
<name>switchbotapi-java</name>
1010
<url>https://github.com/bigboxer23/switchbotapi-java</url>

src/main/java/com/bigboxer23/switch_bot/SwitchBotApi.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javax.crypto.Mac;
1212
import javax.crypto.spec.SecretKeySpec;
1313
import lombok.Getter;
14+
import okhttp3.Response;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
1617

@@ -78,17 +79,17 @@ protected RequestBuilderCallback addAuth() {
7879
* @param apiResponse the API response
7980
* @return true if error occurs
8081
*/
81-
protected boolean checkForError(IApiResponse apiResponse) {
82-
return Optional.ofNullable(apiResponse)
83-
.map(response -> {
84-
if (response.getStatusCode() != 100) {
85-
logger.error("error code: " + response.getStatusCode() + " : " + response.getMessage());
82+
protected boolean checkForError(Response response, Optional<IApiResponse> apiResponse) {
83+
return apiResponse
84+
.map(api -> {
85+
if (api.getStatusCode() != 100) {
86+
logger.error("error code: " + api.getStatusCode() + " : " + api.getMessage());
8687
return false;
8788
}
8889
return true;
8990
})
9091
.orElseGet(() -> {
91-
logger.error("null api response");
92+
logger.error("Error calling switchbot api: " + response.code() + " " + response.message());
9293
return false;
9394
});
9495
}

src/main/java/com/bigboxer23/switch_bot/SwitchBotDeviceApi.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.nio.charset.StandardCharsets;
88
import java.util.List;
99
import java.util.Optional;
10-
1110
import okhttp3.RequestBody;
1211
import okhttp3.Response;
1312
import org.slf4j.Logger;
@@ -78,7 +77,7 @@ public void sendDeviceControlCommands(String deviceId, DeviceCommand command) th
7877
*/
7978
private <T extends IApiResponse> T parseResponse(Response response, Class<T> clazz) throws IOException {
8079
Optional<T> apiResponse = OkHttpUtil.getBody(response, clazz);
81-
if (apiResponse.isEmpty() || !provider.checkForError(apiResponse.get())) {
80+
if (!provider.checkForError(response, (Optional<IApiResponse>) apiResponse)) {
8281
throw new IOException(response.code() + " " + response.message());
8382
}
8483
return apiResponse.get();

0 commit comments

Comments
 (0)