Skip to content

Commit bae9b84

Browse files
chore: adds null check to CacheUpdater
1 parent 4eb979b commit bae9b84

File tree

2 files changed

+10
-34
lines changed

2 files changed

+10
-34
lines changed

api/src/main/java/com/javadiscord/jdi/core/api/DiscordResponseParser.java

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import java.util.List;
44
import java.util.Map;
55

6-
import com.fasterxml.jackson.core.JsonProcessingException;
7-
import com.fasterxml.jackson.databind.ObjectMapper;
8-
import com.javadiscord.jdi.core.api.utils.CacheUpdateHandler;
6+
import com.javadiscord.jdi.core.api.utils.CacheUpdater;
97
import com.javadiscord.jdi.internal.api.DiscordRequest;
108
import com.javadiscord.jdi.internal.api.DiscordRequestDispatcher;
119
import com.javadiscord.jdi.internal.api.DiscordResponse;
@@ -18,11 +16,11 @@
1816
public class DiscordResponseParser {
1917
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
2018
private final DiscordRequestDispatcher dispatcher;
21-
private final CacheUpdateHandler cacheUpdateHandler;
19+
private final CacheUpdater cacheUpdater;
2220

2321
public DiscordResponseParser(DiscordRequestDispatcher dispatcher, Cache cache) {
2422
this.dispatcher = dispatcher;
25-
this.cacheUpdateHandler = new CacheUpdateHandler(cache);
23+
this.cacheUpdater = new CacheUpdater(cache);
2624
}
2725

2826
public <T> AsyncResponse<List<T>> callAndParseList(Class<T> clazz, DiscordRequest request) {
@@ -34,6 +32,7 @@ public <T> AsyncResponse<List<T>> callAndParseList(Class<T> clazz, DiscordReques
3432
try {
3533
List<T> resultList = parseResponseFromList(clazz, response.body());
3634
asyncResponse.setResult(resultList);
35+
cacheUpdater.updateCache(resultList);
3736
} catch (Exception e) {
3837
asyncResponse.setException(e);
3938
}
@@ -42,19 +41,6 @@ public <T> AsyncResponse<List<T>> callAndParseList(Class<T> clazz, DiscordReques
4241
}
4342
}
4443
);
45-
response -> {
46-
if (isSuccessfulResponse(response)) {
47-
try {
48-
List<T> resultList = parseResponseFromList(clazz, response.body());
49-
asyncResponse.setResult(resultList);
50-
cacheUpdateHandler.updateCache(resultList);
51-
} catch (Exception e) {
52-
asyncResponse.setException(e);
53-
}
54-
} else {
55-
asyncResponse.setException(errorResponseException(response));
56-
}
57-
});
5844
future.onError(asyncResponse::setException);
5945
return asyncResponse;
6046
}
@@ -76,19 +62,6 @@ public <T> AsyncResponse<List<T>> callAndParseMap(String key, DiscordRequest req
7662
}
7763
}
7864
);
79-
response -> {
80-
if (isSuccessfulResponse(response)) {
81-
try {
82-
List<T> resultList = parseResponseFromMap(key, response.body());
83-
asyncResponse.setResult(resultList);
84-
cacheUpdateHandler.updateCache(resultList);
85-
} catch (Exception e){
86-
asyncResponse.setException(e);
87-
}
88-
} else {
89-
asyncResponse.setException(errorResponseException(response));
90-
}
91-
});
9265
future.onError(asyncResponse::setException);
9366
return asyncResponse;
9467
}
@@ -137,7 +110,7 @@ private <T> void success(
137110
result = OBJECT_MAPPER.readValue(response.body(), type);
138111
}
139112
asyncResponse.setResult(result);
140-
cacheUpdateHandler.updateCache(result);
113+
cacheUpdater.updateCache(result);
141114
} catch (JsonProcessingException e) {
142115
asyncResponse.setException(e);
143116
}

api/src/main/java/com/javadiscord/jdi/core/api/utils/CacheUpdateHandler.java renamed to api/src/main/java/com/javadiscord/jdi/core/api/utils/CacheUpdater.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
import java.lang.reflect.Field;
66
import java.util.List;
77

8-
public class CacheUpdateHandler {
8+
public class CacheUpdater {
99

1010
private final Cache cache;
1111

12-
public CacheUpdateHandler(Cache cache) {
12+
public CacheUpdater(Cache cache) {
1313
this.cache = cache;
1414
}
1515

1616
public <T> void updateCache(T result) {
17+
if (result == null) {
18+
return;
19+
}
1720
try {
1821
Field guildIdField = result.getClass().getDeclaredField("guildId");
1922
Field idField = result.getClass().getDeclaredField("id");

0 commit comments

Comments
 (0)