Skip to content

AppConfig-v2: update exception logging #45295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sdk/appconfiguration/azure-data-appconfiguration-v2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ Code generated by Microsoft (R) TypeSpec Code Generator.
<jacoco.min.branchcoverage>0.10</jacoco.min.branchcoverage>
</properties>
<dependencies>
<dependency>
<groupId>io.clientcore</groupId>
<artifactId>core</artifactId>
<version>1.0.0-beta.10</version> <!-- {x-version-update;unreleased_io.clientcore:core;dependency} -->
</dependency>
<dependency>
<groupId>com.azure.v2</groupId>
<artifactId>azure-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,8 @@ public Response<ConfigurationSetting> getConfigurationSettingWithResponse(Config
return new Response<>(httpResponse.getRequest(), httpResponse.getStatusCode(),
httpResponse.getHeaders(), null);
}
throw LOGGER.logThrowableAsError(ex);
// HttpResponseException is logged in the instrumentation policy.
throw ex;
}
}

Expand Down Expand Up @@ -994,7 +995,7 @@ public PagedIterable<ConfigurationSetting> listConfigurationSettings(SettingSele
acceptDateTime, settingFields, null, null, getPageETag(matchConditionsList, pageETagIndex),
tagsFilter, context);
} catch (HttpResponseException ex) {
return handleNotModifiedErrorToValidResponse(ex, LOGGER);
return handleNotModifiedErrorToValidResponse(ex);
}
return toConfigurationSettingWithPagedResponse(pagedResponse);
}, (nextLink, ignored) -> {
Expand All @@ -1003,7 +1004,7 @@ acceptDateTime, settingFields, null, null, getPageETag(matchConditionsList, page
pagedResponse = serviceClient.getKeyValuesNextSinglePage(nextLink.getContinuationToken(), null, null,
acceptDateTime, null, getPageETag(matchConditionsList, pageETagIndex), context);
} catch (HttpResponseException ex) {
return handleNotModifiedErrorToValidResponse(ex, LOGGER);
return handleNotModifiedErrorToValidResponse(ex);
}
return toConfigurationSettingWithPagedResponse(pagedResponse);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ public ConfigurationClientBuilder audience(ConfigurationAudience audience) {
private AzureAppConfigurationClientImpl buildInnerClient() {
// Manual changes start
if (isNullOrEmpty(connectionString) && isNullOrEmpty(endpoint)) {
throw LOGGER
.logThrowableAsError(new IllegalArgumentException("'connectionString' or 'endpoint' cannot be null."));
throw LOGGER.throwableAtError()
.log("'connectionString' or 'endpoint' cannot be null.", IllegalArgumentException::new);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.clientcore.core.http.models.HttpHeaders;
import io.clientcore.core.http.models.HttpRequest;
import io.clientcore.core.instrumentation.logging.ClientLogger;
import io.clientcore.core.models.CoreException;
import io.clientcore.core.models.binarydata.BinaryData;
import io.clientcore.core.utils.DateTimeRfc1123;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.clientcore.core.http.models.Response;
import io.clientcore.core.http.paging.PagedResponse;
import io.clientcore.core.instrumentation.logging.ClientLogger;
import io.clientcore.core.models.CoreException;
import io.clientcore.core.serialization.json.JsonReader;
import io.clientcore.core.serialization.json.JsonToken;

Expand Down Expand Up @@ -99,11 +100,12 @@ public static ConfigurationSetting toConfigurationSetting(KeyValue keyValue) {
// Configuration Setting
return setting;
}
} catch (Exception exception) {
throw LOGGER.logThrowableAsError(
new RuntimeException("The setting is neither a 'FeatureFlagConfigurationSetting' nor "
+ "'SecretReferenceConfigurationSetting', return the setting as 'ConfigurationSetting'. "
+ "Error: ", exception));
} catch (RuntimeException exception) {
throw LOGGER.throwableAtError()
.log(
"The setting is neither a 'FeatureFlagConfigurationSetting' nor "
+ "'SecretReferenceConfigurationSetting', return the setting as 'ConfigurationSetting'. ",
exception, CoreException::from);
}
}

Expand All @@ -121,7 +123,7 @@ public static FeatureFlagConfigurationSetting parseFeatureFlagValue(String value
try (JsonReader jsonReader = JsonReader.fromString(valueInJson)) {
return getFeatureFlagPropertyValue(jsonReader);
} catch (IOException e) {
throw LOGGER.logThrowableAsError(new IllegalStateException(e));
throw LOGGER.throwableAtError().log(e, IllegalStateException::new);
}
}

Expand All @@ -145,7 +147,7 @@ public static SecretReferenceConfigurationSetting parseSecretReferenceFieldValue
return new SecretReferenceConfigurationSetting(key, secretId);
});
} catch (IOException e) {
throw LOGGER.logThrowableAsError(new IllegalStateException(e));
throw LOGGER.throwableAtError().log(e, IllegalStateException::new);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public final class SyncToken {
public static SyncToken createSyncToken(String syncToken) {
final SyncToken token = new SyncToken();
if (CoreUtils.isNullOrEmpty(syncToken)) {
throw LOGGER.logThrowableAsError(new IllegalArgumentException(CANNOT_EMPTY_OR_NULL));
throw LOGGER.throwableAtError().log(CANNOT_EMPTY_OR_NULL, IllegalArgumentException::new);
}

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

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

try {
token.sequenceNumber = Long.parseLong(snParts[1]);
} catch (NumberFormatException ex) {
throw LOGGER.logThrowableAsError(new IllegalArgumentException(SEQUENCE_NUMBER_CANNOT_PARSED));
throw LOGGER.throwableAtError().log(SEQUENCE_NUMBER_CANNOT_PARSED, IllegalArgumentException::new);
}
token.id = idParts[0];
token.value = idParts[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public void updateSyncToken(String token) {
final SyncToken syncToken;
try {
syncToken = SyncToken.createSyncToken(syncTokenString);
} catch (Exception ex) {
logger.logThrowableAsWarning(ex);
} catch (RuntimeException ex) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import io.clientcore.core.http.models.HttpResponseException;
import io.clientcore.core.http.models.Response;
import io.clientcore.core.http.paging.PagedResponse;
import io.clientcore.core.instrumentation.logging.ClientLogger;
import io.clientcore.core.models.binarydata.BinaryData;
import io.clientcore.core.utils.CoreUtils;

Expand Down Expand Up @@ -118,20 +117,19 @@ public static String parseNextLink(String nextLink) {
}

// Sync Handler
public static PagedResponse<ConfigurationSetting> handleNotModifiedErrorToValidResponse(HttpResponseException error,
ClientLogger logger) {
public static PagedResponse<ConfigurationSetting>
handleNotModifiedErrorToValidResponse(HttpResponseException error) {
Response<BinaryData> httpResponse = error.getResponse();
if (httpResponse == null) {
throw logger.logThrowableAsError(error);
if (httpResponse != null) {
String continuationToken = parseNextLink(httpResponse.getHeaders().getValue(HttpHeaderName.LINK));
if (httpResponse.getStatusCode() == 304) {
return new PagedResponse<>(httpResponse.getRequest(), httpResponse.getStatusCode(),
httpResponse.getHeaders(), null, continuationToken, null, null, null, null);
}
}

String continuationToken = parseNextLink(httpResponse.getHeaders().getValue(HttpHeaderName.LINK));
if (httpResponse.getStatusCode() == 304) {
return new PagedResponse<>(httpResponse.getRequest(), httpResponse.getStatusCode(),
httpResponse.getHeaders(), null, continuationToken, null, null, null, null);
}

throw logger.logThrowableAsError(error);
// HttpResponseException is already logged in instrumentation policy
throw error;
}

// Get the ETag from a list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

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

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

writer.flush();
newValue = outputStream.toString(StandardCharsets.UTF_8.name());
outputStream.close();
} catch (IOException exception) {
LOGGER.logThrowableAsError(
new IllegalArgumentException("Can't parse Feature Flag configuration setting value.", exception));
throw LOGGER.throwableAtError()
.log("Can't parse Feature Flag configuration setting value.", exception, CoreException::from);
}

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

private void checkValid() {
if (!isValidFeatureFlagValue) {
throw LOGGER.logThrowableAsError(new IllegalArgumentException("The content of the " + super.getValue()
+ " property do not represent a valid feature flag configuration setting."));
throw LOGGER.throwableAtError()
.log(
"The content of the " + super.getValue()
+ " property do not represent a valid feature flag configuration setting.",
IllegalArgumentException::new);
}
}

Expand Down Expand Up @@ -483,7 +485,7 @@ private void tryParseValue(String value) {
});
} catch (IOException e) {
isValidFeatureFlagValue = false;
throw LOGGER.logThrowableAsError(new IllegalArgumentException(e));
throw LOGGER.throwableAtError().log(e, IllegalArgumentException::new);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.azure.v2.data.appconfiguration.models;

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

try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonWriter writer = JsonWriter.toStream(outputStream)) {
boolean isUriWritten = false;

writer.writeStartObject();
Expand All @@ -112,7 +112,7 @@ public String getValue() {
writer.writeUntypedField(name, jsonValue);
}
} catch (IOException e) {
throw LOGGER.logThrowableAsError(new RuntimeException(e));
throw LOGGER.throwableAtError().log(e, CoreException::from);
}
}

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

newValue = outputStream.toString(StandardCharsets.UTF_8.name());
outputStream.close();
} catch (IOException exception) {
LOGGER.logThrowableAsError(
new IllegalArgumentException("Can't parse Secret Reference configuration setting value.", exception));
throw LOGGER.throwableAtError()
.log("Can't parse Secret Reference configuration setting value.", exception, CoreException::from);
}

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

private void checkValid() {
if (!isValidSecretReferenceValue) {
throw LOGGER.logThrowableAsError(new IllegalArgumentException("The content of the " + super.getValue()
+ " property do not represent a valid secret reference configuration setting."));
throw LOGGER.throwableAtError()
.log(
"The content of the " + super.getValue()
+ " property do not represent a valid secret reference configuration setting.",
IllegalArgumentException::new);
}
}

Expand Down Expand Up @@ -239,7 +241,7 @@ private void tryParseValue(String value) {
});
} catch (IOException e) {
isValidSecretReferenceValue = false;
throw LOGGER.logThrowableAsError(new IllegalArgumentException(e));
throw LOGGER.throwableAtError().log(e, IllegalArgumentException::new);
}
}
}