Skip to content

Commit a0c7823

Browse files
authored
AppConfig-v2: update exception logging (#45295)
1 parent ce8b854 commit a0c7823

File tree

10 files changed

+64
-56
lines changed

10 files changed

+64
-56
lines changed

sdk/appconfiguration/azure-data-appconfiguration-v2/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ Code generated by Microsoft (R) TypeSpec Code Generator.
4848
<jacoco.min.branchcoverage>0.10</jacoco.min.branchcoverage>
4949
</properties>
5050
<dependencies>
51+
<dependency>
52+
<groupId>io.clientcore</groupId>
53+
<artifactId>core</artifactId>
54+
<version>1.0.0-beta.10</version> <!-- {x-version-update;unreleased_io.clientcore:core;dependency} -->
55+
</dependency>
5156
<dependency>
5257
<groupId>com.azure.v2</groupId>
5358
<artifactId>azure-core</artifactId>

sdk/appconfiguration/azure-data-appconfiguration-v2/src/main/java/com/azure/v2/data/appconfiguration/ConfigurationClient.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,8 @@ public Response<ConfigurationSetting> getConfigurationSettingWithResponse(Config
657657
return new Response<>(httpResponse.getRequest(), httpResponse.getStatusCode(),
658658
httpResponse.getHeaders(), null);
659659
}
660-
throw LOGGER.logThrowableAsError(ex);
660+
// HttpResponseException is logged in the instrumentation policy.
661+
throw ex;
661662
}
662663
}
663664

@@ -994,7 +995,7 @@ public PagedIterable<ConfigurationSetting> listConfigurationSettings(SettingSele
994995
acceptDateTime, settingFields, null, null, getPageETag(matchConditionsList, pageETagIndex),
995996
tagsFilter, context);
996997
} catch (HttpResponseException ex) {
997-
return handleNotModifiedErrorToValidResponse(ex, LOGGER);
998+
return handleNotModifiedErrorToValidResponse(ex);
998999
}
9991000
return toConfigurationSettingWithPagedResponse(pagedResponse);
10001001
}, (nextLink, ignored) -> {
@@ -1003,7 +1004,7 @@ acceptDateTime, settingFields, null, null, getPageETag(matchConditionsList, page
10031004
pagedResponse = serviceClient.getKeyValuesNextSinglePage(nextLink.getContinuationToken(), null, null,
10041005
acceptDateTime, null, getPageETag(matchConditionsList, pageETagIndex), context);
10051006
} catch (HttpResponseException ex) {
1006-
return handleNotModifiedErrorToValidResponse(ex, LOGGER);
1007+
return handleNotModifiedErrorToValidResponse(ex);
10071008
}
10081009
return toConfigurationSettingWithPagedResponse(pagedResponse);
10091010
});

sdk/appconfiguration/azure-data-appconfiguration-v2/src/main/java/com/azure/v2/data/appconfiguration/ConfigurationClientBuilder.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ public ConfigurationClientBuilder audience(ConfigurationAudience audience) {
302302
private AzureAppConfigurationClientImpl buildInnerClient() {
303303
// Manual changes start
304304
if (isNullOrEmpty(connectionString) && isNullOrEmpty(endpoint)) {
305-
throw LOGGER
306-
.logThrowableAsError(new IllegalArgumentException("'connectionString' or 'endpoint' cannot be null."));
305+
throw LOGGER.throwableAtError()
306+
.log("'connectionString' or 'endpoint' cannot be null.", IllegalArgumentException::new);
307307
}
308308

309309
if (!isNullOrEmpty(connectionString) && isNullOrEmpty(endpoint)) {
@@ -346,8 +346,8 @@ private HttpPipeline createHttpPipeline() {
346346
} else if (connectionStringCredentials != null) {
347347
policies.add(new ConfigurationCredentialsPolicy(connectionStringCredentials));
348348
} else {
349-
throw LOGGER.logThrowableAsError(
350-
new IllegalArgumentException("Missing credential information while building a client."));
349+
throw LOGGER.throwableAtError()
350+
.log("Missing credential information while building a client.", IllegalArgumentException::new);
351351
}
352352
// Manual changes end
353353

sdk/appconfiguration/azure-data-appconfiguration-v2/src/main/java/com/azure/v2/data/appconfiguration/implementation/ConfigurationClientCredentials.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.clientcore.core.http.models.HttpHeaders;
77
import io.clientcore.core.http.models.HttpRequest;
88
import io.clientcore.core.instrumentation.logging.ClientLogger;
9+
import io.clientcore.core.models.CoreException;
910
import io.clientcore.core.models.binarydata.BinaryData;
1011
import io.clientcore.core.utils.DateTimeRfc1123;
1112

@@ -100,10 +101,8 @@ void setAuthorizationHeaders(HttpRequest httpRequest) {
100101
= Base64.getEncoder().encodeToString(sha256HMAC.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8)));
101102
headers.set(HttpHeaderName.AUTHORIZATION, "HMAC-SHA256 Credential=" + credentials.id()
102103
+ "&SignedHeaders=Host;Date;x-ms-content-sha256&Signature=" + signature);
103-
} catch (GeneralSecurityException e) {
104-
throw LOGGER.logThrowableAsError(new RuntimeException(e));
105-
} catch (MalformedURLException e) {
106-
throw new RuntimeException(e);
104+
} catch (GeneralSecurityException | MalformedURLException e) {
105+
throw LOGGER.throwableAtError().log(e, CoreException::from);
107106
}
108107
}
109108

sdk/appconfiguration/azure-data-appconfiguration-v2/src/main/java/com/azure/v2/data/appconfiguration/implementation/ConfigurationSettingDeserializationHelper.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.clientcore.core.http.models.Response;
1212
import io.clientcore.core.http.paging.PagedResponse;
1313
import io.clientcore.core.instrumentation.logging.ClientLogger;
14+
import io.clientcore.core.models.CoreException;
1415
import io.clientcore.core.serialization.json.JsonReader;
1516
import io.clientcore.core.serialization.json.JsonToken;
1617

@@ -99,11 +100,12 @@ public static ConfigurationSetting toConfigurationSetting(KeyValue keyValue) {
99100
// Configuration Setting
100101
return setting;
101102
}
102-
} catch (Exception exception) {
103-
throw LOGGER.logThrowableAsError(
104-
new RuntimeException("The setting is neither a 'FeatureFlagConfigurationSetting' nor "
105-
+ "'SecretReferenceConfigurationSetting', return the setting as 'ConfigurationSetting'. "
106-
+ "Error: ", exception));
103+
} catch (RuntimeException exception) {
104+
throw LOGGER.throwableAtError()
105+
.log(
106+
"The setting is neither a 'FeatureFlagConfigurationSetting' nor "
107+
+ "'SecretReferenceConfigurationSetting', return the setting as 'ConfigurationSetting'. ",
108+
exception, CoreException::from);
107109
}
108110
}
109111

@@ -121,7 +123,7 @@ public static FeatureFlagConfigurationSetting parseFeatureFlagValue(String value
121123
try (JsonReader jsonReader = JsonReader.fromString(valueInJson)) {
122124
return getFeatureFlagPropertyValue(jsonReader);
123125
} catch (IOException e) {
124-
throw LOGGER.logThrowableAsError(new IllegalStateException(e));
126+
throw LOGGER.throwableAtError().log(e, IllegalStateException::new);
125127
}
126128
}
127129

@@ -145,7 +147,7 @@ public static SecretReferenceConfigurationSetting parseSecretReferenceFieldValue
145147
return new SecretReferenceConfigurationSetting(key, secretId);
146148
});
147149
} catch (IOException e) {
148-
throw LOGGER.logThrowableAsError(new IllegalStateException(e));
150+
throw LOGGER.throwableAtError().log(e, IllegalStateException::new);
149151
}
150152
}
151153

sdk/appconfiguration/azure-data-appconfiguration-v2/src/main/java/com/azure/v2/data/appconfiguration/implementation/SyncToken.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public final class SyncToken {
4343
public static SyncToken createSyncToken(String syncToken) {
4444
final SyncToken token = new SyncToken();
4545
if (CoreUtils.isNullOrEmpty(syncToken)) {
46-
throw LOGGER.logThrowableAsError(new IllegalArgumentException(CANNOT_EMPTY_OR_NULL));
46+
throw LOGGER.throwableAtError().log(CANNOT_EMPTY_OR_NULL, IllegalArgumentException::new);
4747
}
4848

4949
final String[] syncTokenParts = syncToken.split(SEMICOLON, 2);
5050
if (syncTokenParts.length != 2) {
51-
throw LOGGER.logThrowableAsError(new IllegalArgumentException(VALID_FORMAT_ERROR_MESSAGE));
51+
throw LOGGER.throwableAtError().log(VALID_FORMAT_ERROR_MESSAGE, IllegalArgumentException::new);
5252
}
5353

5454
final String[] idParts = syncTokenParts[0].split(EQUAL, 2);
@@ -59,13 +59,13 @@ public static SyncToken createSyncToken(String syncToken) {
5959
|| idParts[1].isEmpty()
6060
|| snParts[0].isEmpty()
6161
|| snParts[1].isEmpty()) {
62-
throw LOGGER.logThrowableAsError(new IllegalArgumentException(VALID_FORMAT_ERROR_MESSAGE));
62+
throw LOGGER.throwableAtError().log(VALID_FORMAT_ERROR_MESSAGE, IllegalArgumentException::new);
6363
}
6464

6565
try {
6666
token.sequenceNumber = Long.parseLong(snParts[1]);
6767
} catch (NumberFormatException ex) {
68-
throw LOGGER.logThrowableAsError(new IllegalArgumentException(SEQUENCE_NUMBER_CANNOT_PARSED));
68+
throw LOGGER.throwableAtError().log(SEQUENCE_NUMBER_CANNOT_PARSED, IllegalArgumentException::new);
6969
}
7070
token.id = idParts[0];
7171
token.value = idParts[1];

sdk/appconfiguration/azure-data-appconfiguration-v2/src/main/java/com/azure/v2/data/appconfiguration/implementation/SyncTokenPolicy.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ public void updateSyncToken(String token) {
6161
final SyncToken syncToken;
6262
try {
6363
syncToken = SyncToken.createSyncToken(syncTokenString);
64-
} catch (Exception ex) {
65-
logger.logThrowableAsWarning(ex);
64+
} catch (RuntimeException ex) {
6665
continue;
6766
}
6867

sdk/appconfiguration/azure-data-appconfiguration-v2/src/main/java/com/azure/v2/data/appconfiguration/implementation/Utility.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import io.clientcore.core.http.models.HttpResponseException;
1212
import io.clientcore.core.http.models.Response;
1313
import io.clientcore.core.http.paging.PagedResponse;
14-
import io.clientcore.core.instrumentation.logging.ClientLogger;
1514
import io.clientcore.core.models.binarydata.BinaryData;
1615
import io.clientcore.core.utils.CoreUtils;
1716

@@ -118,20 +117,19 @@ public static String parseNextLink(String nextLink) {
118117
}
119118

120119
// Sync Handler
121-
public static PagedResponse<ConfigurationSetting> handleNotModifiedErrorToValidResponse(HttpResponseException error,
122-
ClientLogger logger) {
120+
public static PagedResponse<ConfigurationSetting>
121+
handleNotModifiedErrorToValidResponse(HttpResponseException error) {
123122
Response<BinaryData> httpResponse = error.getResponse();
124-
if (httpResponse == null) {
125-
throw logger.logThrowableAsError(error);
123+
if (httpResponse != null) {
124+
String continuationToken = parseNextLink(httpResponse.getHeaders().getValue(HttpHeaderName.LINK));
125+
if (httpResponse.getStatusCode() == 304) {
126+
return new PagedResponse<>(httpResponse.getRequest(), httpResponse.getStatusCode(),
127+
httpResponse.getHeaders(), null, continuationToken, null, null, null, null);
128+
}
126129
}
127130

128-
String continuationToken = parseNextLink(httpResponse.getHeaders().getValue(HttpHeaderName.LINK));
129-
if (httpResponse.getStatusCode() == 304) {
130-
return new PagedResponse<>(httpResponse.getRequest(), httpResponse.getStatusCode(),
131-
httpResponse.getHeaders(), null, continuationToken, null, null, null, null);
132-
}
133-
134-
throw logger.logThrowableAsError(error);
131+
// HttpResponseException is already logged in instrumentation policy
132+
throw error;
135133
}
136134

137135
// Get the ETag from a list

sdk/appconfiguration/azure-data-appconfiguration-v2/src/main/java/com/azure/v2/data/appconfiguration/models/FeatureFlagConfigurationSetting.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.azure.v2.data.appconfiguration.implementation.Conditions;
77
import io.clientcore.core.instrumentation.logging.ClientLogger;
8+
import io.clientcore.core.models.CoreException;
89
import io.clientcore.core.serialization.json.JsonReader;
910
import io.clientcore.core.serialization.json.JsonToken;
1011
import io.clientcore.core.serialization.json.JsonWriter;
@@ -84,10 +85,9 @@ public FeatureFlagConfigurationSetting(String featureId, boolean isEnabled) {
8485
public String getValue() {
8586
// Lazily update: Update 'value' by all latest property values when this getValue() method is called.
8687
String newValue = null;
87-
try {
88-
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
89-
final JsonWriter writer = JsonWriter.toStream(outputStream);
9088

89+
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
90+
JsonWriter writer = JsonWriter.toStream(outputStream)) {
9191
final Set<String> knownProperties = new LinkedHashSet<>(requiredOrOptionalJsonProperties);
9292

9393
writer.writeStartObject();
@@ -105,7 +105,7 @@ public String getValue() {
105105
writer.writeUntypedField(name, jsonValue);
106106
}
107107
} catch (IOException e) {
108-
throw LOGGER.logThrowableAsError(new RuntimeException(e));
108+
throw LOGGER.throwableAtError().log(e, CoreException::from);
109109
}
110110
}
111111
// Remaining known properties we are not processed yet after 'parsedProperties'.
@@ -116,10 +116,9 @@ public String getValue() {
116116

117117
writer.flush();
118118
newValue = outputStream.toString(StandardCharsets.UTF_8.name());
119-
outputStream.close();
120119
} catch (IOException exception) {
121-
LOGGER.logThrowableAsError(
122-
new IllegalArgumentException("Can't parse Feature Flag configuration setting value.", exception));
120+
throw LOGGER.throwableAtError()
121+
.log("Can't parse Feature Flag configuration setting value.", exception, CoreException::from);
123122
}
124123

125124
super.setValue(newValue);
@@ -361,8 +360,11 @@ public FeatureFlagConfigurationSetting addClientFilter(FeatureFlagFilter clientF
361360

362361
private void checkValid() {
363362
if (!isValidFeatureFlagValue) {
364-
throw LOGGER.logThrowableAsError(new IllegalArgumentException("The content of the " + super.getValue()
365-
+ " property do not represent a valid feature flag configuration setting."));
363+
throw LOGGER.throwableAtError()
364+
.log(
365+
"The content of the " + super.getValue()
366+
+ " property do not represent a valid feature flag configuration setting.",
367+
IllegalArgumentException::new);
366368
}
367369
}
368370

@@ -483,7 +485,7 @@ private void tryParseValue(String value) {
483485
});
484486
} catch (IOException e) {
485487
isValidFeatureFlagValue = false;
486-
throw LOGGER.logThrowableAsError(new IllegalArgumentException(e));
488+
throw LOGGER.throwableAtError().log(e, IllegalArgumentException::new);
487489
}
488490
}
489491
}

sdk/appconfiguration/azure-data-appconfiguration-v2/src/main/java/com/azure/v2/data/appconfiguration/models/SecretReferenceConfigurationSetting.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.azure.v2.data.appconfiguration.models;
55

66
import io.clientcore.core.instrumentation.logging.ClientLogger;
7+
import io.clientcore.core.models.CoreException;
78
import io.clientcore.core.serialization.json.JsonReader;
89
import io.clientcore.core.serialization.json.JsonToken;
910
import io.clientcore.core.serialization.json.JsonWriter;
@@ -90,10 +91,9 @@ public SecretReferenceConfigurationSetting setKey(String key) {
9091
public String getValue() {
9192
// Lazily update: Update 'value' by all latest property values when this getValue() method is called.
9293
String newValue = null;
93-
try {
94-
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
95-
final JsonWriter writer = JsonWriter.toStream(outputStream);
9694

95+
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
96+
JsonWriter writer = JsonWriter.toStream(outputStream)) {
9797
boolean isUriWritten = false;
9898

9999
writer.writeStartObject();
@@ -112,7 +112,7 @@ public String getValue() {
112112
writer.writeUntypedField(name, jsonValue);
113113
}
114114
} catch (IOException e) {
115-
throw LOGGER.logThrowableAsError(new RuntimeException(e));
115+
throw LOGGER.throwableAtError().log(e, CoreException::from);
116116
}
117117
}
118118

@@ -124,10 +124,9 @@ public String getValue() {
124124
writer.flush();
125125

126126
newValue = outputStream.toString(StandardCharsets.UTF_8.name());
127-
outputStream.close();
128127
} catch (IOException exception) {
129-
LOGGER.logThrowableAsError(
130-
new IllegalArgumentException("Can't parse Secret Reference configuration setting value.", exception));
128+
throw LOGGER.throwableAtError()
129+
.log("Can't parse Secret Reference configuration setting value.", exception, CoreException::from);
131130
}
132131

133132
super.setValue(newValue);
@@ -201,8 +200,11 @@ public SecretReferenceConfigurationSetting setTags(Map<String, String> tags) {
201200

202201
private void checkValid() {
203202
if (!isValidSecretReferenceValue) {
204-
throw LOGGER.logThrowableAsError(new IllegalArgumentException("The content of the " + super.getValue()
205-
+ " property do not represent a valid secret reference configuration setting."));
203+
throw LOGGER.throwableAtError()
204+
.log(
205+
"The content of the " + super.getValue()
206+
+ " property do not represent a valid secret reference configuration setting.",
207+
IllegalArgumentException::new);
206208
}
207209
}
208210

@@ -239,7 +241,7 @@ private void tryParseValue(String value) {
239241
});
240242
} catch (IOException e) {
241243
isValidSecretReferenceValue = false;
242-
throw LOGGER.logThrowableAsError(new IllegalArgumentException(e));
244+
throw LOGGER.throwableAtError().log(e, IllegalArgumentException::new);
243245
}
244246
}
245247
}

0 commit comments

Comments
 (0)