Skip to content

Exception Improvements #254

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 3 commits into from
Jun 30, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private AuthenticationResult acquireTokenWithDeviceCode(DeviceCode deviceCode,
try {
return acquireTokenByAuthorisationGrantSupplier.execute();
} catch (MsalServiceException ex) {
if (ex.errorCode().equals(AUTHORIZATION_PENDING)) {
if (ex.errorCode() != null && ex.errorCode().equals(AUTHORIZATION_PENDING)) {
TimeUnit.SECONDS.sleep(deviceCode.interval());
} else {
throw ex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public IAuthenticationResult get() {
String error = StringHelper.EMPTY_STRING;
if (ex instanceof MsalException) {
MsalException exception = ((MsalException) ex);
if(exception.errorCode() != null){
if (exception.errorCode() != null){
apiEvent.setApiErrorCode(exception.errorCode());
}
} else {
if(ex.getCause() != null){
if (ex.getCause() != null){
error = ex.getCause().toString();
}
}
Expand Down Expand Up @@ -136,7 +136,7 @@ private void logException(Exception ex) {

if (ex instanceof MsalClientException) {
MsalClientException exception = (MsalClientException) ex;
if (exception.errorCode().equalsIgnoreCase(AuthenticationErrorCode.CACHE_MISS)) {
if (exception.errorCode() != null && exception.errorCode().equalsIgnoreCase(AuthenticationErrorCode.CACHE_MISS)) {
clientApplication.log.debug(logMessage, ex);
return;
}
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/com/microsoft/aad/msal4j/TokenResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ static Long getLongValue(JSONObject jsonObject, String key) throws ParseExceptio
static TokenResponse parseJsonObject(final JSONObject jsonObject)
throws ParseException {

final AccessToken accessToken = AccessToken.parse(jsonObject);
final RefreshToken refreshToken = RefreshToken.parse(jsonObject);

// In same cases such as client credentials there isn't an id token. Instead of a null value
// use an empty string in order to avoid an IllegalArgumentException from OIDCTokens.
String idTokenValue = "";
Expand Down Expand Up @@ -93,7 +90,16 @@ static TokenResponse parseJsonObject(final JSONObject jsonObject)
foci = JSONObjectUtils.getString(jsonObject, "foci");
}

return new TokenResponse(accessToken, refreshToken,idTokenValue, scopeValue, clientInfo,
expiresIn, ext_expires_in, foci);
try {
final AccessToken accessToken = AccessToken.parse(jsonObject);
final RefreshToken refreshToken = RefreshToken.parse(jsonObject);
return new TokenResponse(accessToken, refreshToken, idTokenValue, scopeValue, clientInfo,
expiresIn, ext_expires_in, foci);
} catch (ParseException e) {
throw new MsalClientException("Invalid or missing token, could not parse. If using B2C, information on a potential B2C issue and workaround can be found here: https://aka.ms/msal4j-b2c-known-issues",
AuthenticationErrorCode.INVALID_JSON);
} catch (Exception e) {
throw new MsalClientException(e);
}
}
}