Skip to content

Commit e6152cc

Browse files
committed
Improve error handling
1 parent 71151f8 commit e6152cc

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
21+
import static com.google.common.base.Preconditions.checkState;
2122

2223
import com.google.api.client.http.HttpRequestFactory;
2324
import com.google.api.client.http.HttpResponseInterceptor;
@@ -49,7 +50,6 @@ final class FirebaseRemoteConfigClientImpl implements FirebaseRemoteConfigClient
4950

5051
private static final Map<String, String> COMMON_HEADERS =
5152
ImmutableMap.of(
52-
"X-GOOG-API-FORMAT-VERSION", "2",
5353
"X-Firebase-Client", "fire-admin-java/" + SdkUtils.getVersion(),
5454
// There is a known issue in which the ETag is not properly returned in cases
5555
// where the request does not specify a compression type. Currently, it is
@@ -95,17 +95,20 @@ public RemoteConfigTemplate getTemplate() throws FirebaseRemoteConfigException {
9595
.addAllHeaders(COMMON_HEADERS);
9696
IncomingHttpResponse response = httpClient.send(request);
9797
RemoteConfigTemplate parsed = httpClient.parse(response, RemoteConfigTemplate.class);
98+
parsed.setETag(getETag(response));
99+
return parsed;
100+
}
98101

102+
private String getETag(IncomingHttpResponse response) {
99103
List<String> etagList = (List<String>) response.getHeaders().get("etag");
100-
checkArgument(!(etagList == null || etagList.isEmpty()),
104+
checkState(etagList != null && !etagList.isEmpty(),
101105
"ETag header is not available in the server response.");
102106

103107
String etag = etagList.get(0);
104-
checkArgument(!Strings.isNullOrEmpty(etag),
108+
checkState(!Strings.isNullOrEmpty(etag),
105109
"ETag header is not available in the server response.");
106110

107-
parsed.setETag(etag);
108-
return parsed;
111+
return etag;
109112
}
110113

111114
static FirebaseRemoteConfigClientImpl fromApp(FirebaseApp app) {

src/main/java/com/google/firebase/remoteconfig/internal/RemoteConfigServiceErrorResponse.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import com.google.firebase.remoteconfig.RemoteConfigErrorCode;
2525

2626
import java.util.Map;
27+
import java.util.regex.Matcher;
28+
import java.util.regex.Pattern;
2729

2830
/**
2931
* The DTO for parsing error responses from the Remote Config service.
@@ -41,14 +43,6 @@ public final class RemoteConfigServiceErrorResponse extends GenericJson {
4143
@Key("error")
4244
private Map<String, Object> error;
4345

44-
public String getStatus() {
45-
if (error == null) {
46-
return null;
47-
}
48-
49-
return (String) error.get("status");
50-
}
51-
5246
@Nullable
5347
public RemoteConfigErrorCode getRemoteConfigErrorCode() {
5448
if (error == null) {
@@ -60,9 +54,9 @@ public RemoteConfigErrorCode getRemoteConfigErrorCode() {
6054
return null;
6155
}
6256

63-
int separator = message.indexOf(':');
64-
if (separator != -1) {
65-
String errorCode = message.substring(0, separator).replaceAll("\\[|\\]", "");
57+
Matcher errorMatcher = Pattern.compile("^\\[(\\w+)\\]:.*$").matcher(message);
58+
if (errorMatcher.find()) {
59+
String errorCode = errorMatcher.group(1);
6660
return RC_ERROR_CODES.get(errorCode);
6761
}
6862

src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void testGetTemplate() throws Exception {
8989
checkGetRequestHeader(interceptor.getLastRequest());
9090
}
9191

92-
@Test(expected = IllegalArgumentException.class)
92+
@Test(expected = IllegalStateException.class)
9393
public void testGetTemplateWithInvalidEtags() throws FirebaseRemoteConfigException {
9494
// ETag does not exist
9595
response.setContent(MOCK_TEMPLATE_RESPONSE);
@@ -290,7 +290,6 @@ private void checkGetRequestHeader(HttpRequest request) {
290290
assertEquals("GET", request.getRequestMethod());
291291
assertEquals(TEST_REMOTE_CONFIG_URL, request.getUrl().toString());
292292
HttpHeaders headers = request.getHeaders();
293-
assertEquals("2", headers.get("X-GOOG-API-FORMAT-VERSION"));
294293
assertEquals("fire-admin-java/" + SdkUtils.getVersion(), headers.get("X-Firebase-Client"));
295294
assertEquals("gzip", headers.getAcceptEncoding());
296295
}

0 commit comments

Comments
 (0)