Skip to content

Commit 4e33ed1

Browse files
committed
Add suberror for network errors
1 parent 97a2615 commit 4e33ed1

34 files changed

+269
-189
lines changed

common/src/main/java/com/microsoft/identity/common/internal/migration/AdalMigrationAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.gson.JsonObject;
3636
import com.google.gson.JsonSyntaxException;
3737
import com.microsoft.identity.common.adal.internal.AuthenticationConstants;
38+
import com.microsoft.identity.common.java.exception.ClientException;
3839
import com.microsoft.identity.common.java.exception.ServiceException;
3940
import com.microsoft.identity.common.adal.internal.cache.ADALTokenCacheItem;
4041
import com.microsoft.identity.common.java.providers.microsoft.MicrosoftAccount;
@@ -239,7 +240,7 @@ public static boolean loadCloudDiscoveryMetadata() {
239240
if (!AzureActiveDirectory.isInitialized()) {
240241
try {
241242
AzureActiveDirectory.performCloudDiscovery();
242-
} catch (final IOException | URISyntaxException e) {
243+
} catch (final ClientException e) {
243244
Logger.error(
244245
methodTag,
245246
"Failed to load instance discovery metadata",

common/src/main/java/com/microsoft/identity/common/internal/result/AdalBrokerResultAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ private void setServiceExceptionPropertiesToBundle(@NonNull final Bundle resultB
295295
// so adding values to these constants as well
296296
resultBundle.putString(AuthenticationConstants.OAuth2.ERROR, serviceException.getErrorCode());
297297
resultBundle.putString(AuthenticationConstants.OAuth2.ERROR_DESCRIPTION, serviceException.getMessage());
298-
resultBundle.putString(AuthenticationConstants.OAuth2.SUBERROR, serviceException.getOAuthSubErrorCode());
298+
resultBundle.putString(AuthenticationConstants.OAuth2.SUBERROR, serviceException.getSubErrorCode());
299299

300300
if (null != serviceException.getHttpResponseBody()) {
301301
resultBundle.putSerializable(

common/src/main/java/com/microsoft/identity/common/internal/result/MsalBrokerResultAdapter.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
import com.microsoft.identity.common.java.opentelemetry.OTelUtility;
7777
import com.microsoft.identity.common.java.opentelemetry.SpanExtension;
7878
import com.microsoft.identity.common.java.opentelemetry.SpanName;
79-
import com.microsoft.identity.common.java.providers.microsoft.azureactivedirectory.ClientInfo;
8079
import com.microsoft.identity.common.java.providers.microsoft.microsoftsts.MicrosoftStsAuthorizationResult;
8180
import com.microsoft.identity.common.java.providers.oauth2.AuthorizationResult;
8281
import com.microsoft.identity.common.java.request.SdkType;
@@ -318,7 +317,7 @@ public Bundle bundleFromBaseException(@NonNull final BaseException exception,
318317

319318
if (exception instanceof ServiceException) {
320319
final ServiceException serviceException = (ServiceException) exception;
321-
builder.oauthSubErrorCode(serviceException.getOAuthSubErrorCode())
320+
builder.oauthSubErrorCode(serviceException.getSubErrorCode())
322321
.httpStatusCode(serviceException.getHttpStatusCode())
323322
.httpResponseBody(AuthenticationSchemeTypeAdapter.getGsonInstance().toJson(
324323
serviceException.getHttpResponseBody()));
@@ -522,6 +521,7 @@ private BaseException getBaseExceptionFromExceptionType(@NonNull final String ex
522521
);
523522
}
524523

524+
baseException.setSubErrorCode(brokerResult.getSubErrorCode());
525525
baseException.setCliTelemErrorCode(brokerResult.getCliTelemErrorCode());
526526
baseException.setCliTelemSubErrorCode(brokerResult.getCliTelemSubErrorCode());
527527
baseException.setCorrelationId(brokerResult.getCorrelationId());
@@ -595,6 +595,7 @@ private BaseException getBaseExceptionFromErrorCodes(@NonNull final BrokerResult
595595
);
596596
}
597597

598+
baseException.setSubErrorCode(brokerResult.getSubErrorCode());
598599
baseException.setCliTelemErrorCode(brokerResult.getCliTelemErrorCode());
599600
baseException.setCliTelemSubErrorCode(brokerResult.getCliTelemSubErrorCode());
600601
baseException.setCorrelationId(brokerResult.getCorrelationId());
@@ -620,7 +621,7 @@ private IntuneAppProtectionPolicyRequiredException getIntuneProtectionRequiredEx
620621
exception.setAuthorityUrl(brokerResult.getAuthority());
621622
exception.setAccountUserId(brokerResult.getLocalAccountId());
622623
exception.setAccountUpn(brokerResult.getUserName());
623-
exception.setOauthSubErrorCode(brokerResult.getSubErrorCode());
624+
exception.setSubErrorCode(brokerResult.getSubErrorCode());
624625
try {
625626
exception.setHttpResponseBody(HashMapExtensions.jsonStringAsMap(
626627
brokerResult.getHttpResponseBody())
@@ -649,8 +650,6 @@ private ServiceException getServiceException(@NonNull final BrokerResult brokerR
649650
null
650651
);
651652

652-
serviceException.setOauthSubErrorCode(brokerResult.getSubErrorCode());
653-
654653
try {
655654
serviceException.setHttpResponseBody(
656655
brokerResult.getHttpResponseBody() != null ?
@@ -686,7 +685,7 @@ private UiRequiredException getUiRequiredException(@NonNull final BrokerResult b
686685
);
687686
if (OAuth2ErrorCode.INTERACTION_REQUIRED.equalsIgnoreCase(errorCode) ||
688687
OAuth2ErrorCode.INVALID_GRANT.equalsIgnoreCase(errorCode)) {
689-
exception.setOauthSubErrorCode(brokerResult.getSubErrorCode());
688+
exception.setSubErrorCode(brokerResult.getSubErrorCode());
690689
}
691690
return exception;
692691
}

common/src/test/java/com/microsoft/identity/common/internal/authorities/AzureActiveDirectoryAuthorityTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
@RunWith(RobolectricTestRunner.class)
4545
public class AzureActiveDirectoryAuthorityTests {
4646
@Test
47-
public void isSameCloudAsAuthority_Returns_True_For_Authority_With_ValidAliases_For_SameCloud() throws IOException, URISyntaxException {
47+
public void isSameCloudAsAuthority_Returns_True_For_Authority_With_ValidAliases_For_SameCloud() throws ClientException {
4848
final String[] cloudAliasesWW = new String[]{"https://login.microsoftonline.com", "https://login.windows.net", "https://login.microsoft.com", "https://sts.windows.net"};
4949
final String[] cloudAliasesCN = new String[]{"https://login.chinacloudapi.cn", "https://login.partner.microsoftonline.cn"};
5050
final String[] cloudAliasesUSGov = new String[]{"https://login.microsoftonline.us", "https://login.usgovcloudapi.net"};
@@ -69,7 +69,7 @@ public void isSameCloudAsAuthority_Returns_True_For_Authority_With_ValidAliases_
6969
}
7070

7171
@Test
72-
public void isSameCloudAsAuthority_Returns_False_For_Authorities_From_Different_Clouds() throws IOException, URISyntaxException {
72+
public void isSameCloudAsAuthority_Returns_False_For_Authorities_From_Different_Clouds() throws ClientException {
7373
final AzureActiveDirectoryAuthority authorityWW = new AzureActiveDirectoryAuthority(new AllAccounts("https://login.microsoftonline.com"));
7474
final AzureActiveDirectoryAuthority authorityCN = new AzureActiveDirectoryAuthority(new AllAccounts("https://login.partner.microsoftonline.cn"));
7575
final AzureActiveDirectoryAuthority authorityUSGov = new AzureActiveDirectoryAuthority(new AllAccounts("https://login.microsoftonline.us"));

common4j/src/main/com/microsoft/identity/common/java/authorities/Authority.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public int hashCode() {
303303
private static final Object sLock = new Object();
304304

305305
private static void performCloudDiscovery()
306-
throws IOException, URISyntaxException {
306+
throws ClientException {
307307
final String methodName = ":performCloudDiscovery";
308308
Logger.verbose(
309309
TAG + methodName,
@@ -389,18 +389,8 @@ public static KnownAuthorityResult getKnownAuthorityResult(Authority authority)
389389

390390
try {
391391
performCloudDiscovery();
392-
} catch (final IOException ex) {
393-
clientException = new ClientException(
394-
ClientException.IO_ERROR,
395-
"Unable to perform cloud discovery",
396-
ex
397-
);
398-
} catch (final URISyntaxException ex) {
399-
clientException = new ClientException(
400-
ClientException.MALFORMED_URL,
401-
"Unable to construct cloud discovery URL",
402-
ex
403-
);
392+
} catch (final ClientException ex) {
393+
clientException = ex;
404394
}
405395

406396
Logger.info(TAG + methodName, "Cloud discovery complete.");

common4j/src/main/com/microsoft/identity/common/java/authorities/AzureActiveDirectoryAuthority.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import com.microsoft.identity.common.java.util.CommonURIBuilder;
3939
import com.microsoft.identity.common.java.util.StringUtil;
4040

41-
import java.io.IOException;
4241
import java.net.MalformedURLException;
4342
import java.net.URI;
4443
import java.net.URISyntaxException;
@@ -177,7 +176,7 @@ public OAuth2Strategy createOAuth2Strategy(@NonNull final OAuth2StrategyParamete
177176
*/
178177
//@WorkerThread
179178
public synchronized boolean isSameCloudAsAuthority(@NonNull final AzureActiveDirectoryAuthority authorityToCheck)
180-
throws IOException, URISyntaxException {
179+
throws ClientException {
181180
if (!AzureActiveDirectory.isInitialized()) {
182181
// Cloud discovery is needed in order to make sure that we have a preferred_network_host_name to cloud aliases mappings
183182
AzureActiveDirectory.performCloudDiscovery();

common4j/src/main/com/microsoft/identity/common/java/commands/parameters/SilentTokenCommandParameters.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
import com.microsoft.identity.common.java.exception.ClientException;
3232
import com.microsoft.identity.common.java.logging.Logger;
3333

34-
import java.io.IOException;
35-
import java.net.URISyntaxException;
36-
3734
import lombok.EqualsAndHashCode;
3835
import lombok.Getter;
3936
import lombok.experimental.SuperBuilder;
@@ -90,12 +87,9 @@ private boolean authorityMatchesAccountEnvironment() {
9087
}
9188
final AzureActiveDirectoryCloud cloud = AzureActiveDirectory.getAzureActiveDirectoryCloudFromHostName(getAccount().getEnvironment());
9289
return cloud != null && cloud.getPreferredNetworkHostName().equals(getAuthority().getAuthorityURL().getAuthority());
93-
} catch (final IOException e) {
94-
cause = e;
95-
errorCode = ClientException.IO_ERROR;
96-
} catch (final URISyntaxException e) {
90+
} catch (final ClientException e) {
9791
cause = e;
98-
errorCode = ClientException.MALFORMED_URL;
92+
errorCode = e.getErrorCode();
9993
}
10094

10195
Logger.error(
@@ -109,7 +103,7 @@ private boolean authorityMatchesAccountEnvironment() {
109103
}
110104

111105
private static void performCloudDiscovery()
112-
throws IOException, URISyntaxException {
106+
throws ClientException {
113107
final String methodName = ":performCloudDiscovery";
114108
Logger.verbose(
115109
TAG + methodName,

common4j/src/main/com/microsoft/identity/common/java/controllers/ExceptionAdapter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ else if (shouldBeConvertedToUiRequiredException(errorResponse.getError())) {
261261
null);
262262
}
263263

264-
outErr.setOauthSubErrorCode(errorResponse.getSubError());
264+
outErr.setSubErrorCode(errorResponse.getSubError());
265265
setHttpResponseUsingTokenErrorResponse(outErr, errorResponse);
266266
return outErr;
267267
}
@@ -282,7 +282,7 @@ public static ServiceException convertToNativeAuthException(@NonNull final Servi
282282
exception.getHttpStatusCode(),
283283
exception
284284
);
285-
outErr.setOauthSubErrorCode(exception.getOAuthSubErrorCode());
285+
outErr.setSubErrorCode(exception.getSubErrorCode());
286286
outErr.setHttpResponseHeaders(exception.getHttpResponseHeaders());
287287
outErr.setHttpResponseBody(exception.getHttpResponseBody());
288288
return outErr;
@@ -311,7 +311,7 @@ public static ServiceException getExceptionFromTokenErrorResponse(@Nullable fina
311311
(BrokerSilentTokenCommandParameters) commandParameters
312312
);
313313
}
314-
policyRequiredException.setOauthSubErrorCode(errorResponse.getSubError());
314+
policyRequiredException.setSubErrorCode(errorResponse.getSubError());
315315
setHttpResponseUsingTokenErrorResponse(policyRequiredException, errorResponse);
316316

317317
return policyRequiredException;

common4j/src/main/com/microsoft/identity/common/java/exception/BaseException.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public class BaseException extends Exception implements IErrorInformation, ITele
6969

7070
private String mErrorCode;
7171

72+
private String mSubErrorCode;
73+
7274
private String mCorrelationId;
7375

7476
// The username of the account that owns the flow.
@@ -140,6 +142,15 @@ public String getErrorCode() {
140142
return mErrorCode;
141143
}
142144

145+
public String getSubErrorCode() { return mSubErrorCode; }
146+
147+
/**
148+
* @param subErrorCode - The sub error code for the exception.
149+
*/
150+
public void setSubErrorCode(@Nullable final String subErrorCode) {
151+
mSubErrorCode = subErrorCode;
152+
}
153+
143154
/**
144155
* {@inheritDoc}
145156
* Return the detailed description explaining why the exception is returned back.

common4j/src/main/com/microsoft/identity/common/java/exception/ClientException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public class ClientException extends BaseException {
7373
public static final String MULTIPLE_MATCHING_TOKENS_DETECTED = "multiple_matching_tokens_detected";
7474

7575
/**
76-
* No active network is available on the device.
76+
* Failed to make a network request from the device.
77+
* See {@link BaseException#getSubErrorCode()} for more details.
7778
*/
7879
public static final String DEVICE_NETWORK_NOT_AVAILABLE = "device_network_not_available";
7980

0 commit comments

Comments
 (0)