Skip to content

Commit

Permalink
Refactor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Rashmini committed Sep 25, 2023
1 parent c65b79e commit 3fc7797
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

package org.wso2.carbon.identity.rest.api.user.recovery.v2.impl.core;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.api.user.common.Util;
import org.wso2.carbon.identity.api.user.recovery.commons.UserAccountRecoveryServiceDataHolder;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.governance.service.notification.NotificationChannels;
import org.wso2.carbon.identity.recovery.IdentityRecoveryClientException;
import org.wso2.carbon.identity.recovery.IdentityRecoveryConstants;
Expand Down Expand Up @@ -54,6 +56,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

/**
Expand Down Expand Up @@ -85,8 +88,44 @@ public Response initiatePasswordRecovery(InitRequest initRequest) {
return Response.ok().entity(buildPasswordRecoveryInitResponse(tenantDomain, recoveryInformationDTO))
.build();
} catch (IdentityRecoveryClientException e) {
throw RecoveryUtil.handleClientException(e, tenantDomain,
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO, Util.getCorrelation());
if (StringUtils.isEmpty(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, e.getErrorCode(), Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
String errorCode = RecoveryUtil.prependOperationScenarioToErrorCode(e.getErrorCode(),
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO);
if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_DISABLED_ACCOUNT.getCode().equals(e.getErrorCode())
|| IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_LOCKED_ACCOUNT.getCode().equals(
e.getErrorCode()) || IdentityRecoveryConstants.ErrorMessages.
ERROR_CODE_PASSWORD_RECOVERY_NOT_ENABLED.getCode().equals(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_FORBIDDEN_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.FORBIDDEN);
} else if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_MULTIPLE_MATCHING_USERS.getCode().equals(
e.getErrorCode())) {
// If user notify is not enabled, throw a accepted response.
if (!Boolean.parseBoolean(IdentityUtil
.getProperty(IdentityRecoveryConstants.ConnectorConfig.NOTIFY_USER_EXISTENCE))) {
throw new WebApplicationException(Response.accepted().build());
}
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
} else if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_NO_USER_FOUND.getCode().equals(
e.getErrorCode())) {
// If user notify is not enabled, throw a accepted response.
if (!Boolean.parseBoolean(IdentityUtil
.getProperty(IdentityRecoveryConstants.ConnectorConfig.NOTIFY_USER_EXISTENCE))) {
throw new WebApplicationException(Response.accepted().build());
}
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_NOT_FOUND_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.NOT_FOUND);
} else if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_NO_VERIFIED_CHANNELS_FOR_USER.
getCode().equals(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_NOT_FOUND_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.NOT_FOUND);
} else {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
} catch (IdentityRecoveryException e) {
throw RecoveryUtil.handleException(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Expand Down Expand Up @@ -124,8 +163,31 @@ public Response recoverPassword(RecoveryRequest recoveryRequest) {
return buildPasswordRecoveryResponse(tenantDomain, passwordRecoverDTO.getNotificationChannel(),
passwordRecoverDTO);
} catch (IdentityRecoveryClientException e) {
throw RecoveryUtil.handleClientException(e, tenantDomain,
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO, Util.getCorrelation());
if (StringUtils.isEmpty(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, e.getErrorCode(), Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
String errorCode = RecoveryUtil.prependOperationScenarioToErrorCode(e.getErrorCode(),
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO);
if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_PASSWORD_RECOVERY_WITH_NOTIFICATIONS_NOT_ENABLED.
getCode().equals(e.getErrorCode()) || IdentityRecoveryConstants.ErrorMessages.
ERROR_CODE_DISABLED_ACCOUNT.getCode().equals(e.getErrorCode()) || IdentityRecoveryConstants.
ErrorMessages.ERROR_CODE_LOCKED_ACCOUNT.getCode().equals(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_FORBIDDEN_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.FORBIDDEN);
} else if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_NO_ACCOUNT_RECOVERY_DATA.getCode().equals(
e.getErrorCode())) {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_NOT_FOUND_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.NOT_FOUND);
} else if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_INVALID_RECOVERY_CODE.getCode().equals(
e.getErrorCode()) || IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_EXPIRED_RECOVERY_CODE
.getCode().equals(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_METHOD_NOT_ACCEPTED_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.NOT_ACCEPTABLE);
} else {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
} catch (IdentityRecoveryException e) {
throw RecoveryUtil.handleException(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Expand All @@ -152,8 +214,29 @@ public Response confirmRecovery(ConfirmRequest confirmRequest) {
RecoveryUtil.buildPropertiesMap(confirmRequest.getProperties()));
return Response.ok().entity(buildResetCodeResponse(tenantDomain, passwordResetCodeDTO)).build();
} catch (IdentityRecoveryClientException e) {
throw RecoveryUtil.handleClientException(e, tenantDomain,
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO, Util.getCorrelation());
if (StringUtils.isEmpty(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, e.getErrorCode(), Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
String errorCode = RecoveryUtil.prependOperationScenarioToErrorCode(e.getErrorCode(),
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO);
if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_USER_TENANT_DOMAIN_MISS_MATCH_WITH_CONTEXT.getCode().
equals(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
} else if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_NO_ACCOUNT_RECOVERY_DATA.getCode().equals(
e.getErrorCode())) {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_NOT_FOUND_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.NOT_FOUND);
} else if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_INVALID_RECOVERY_CODE.getCode().equals(
e.getErrorCode()) || IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_EXPIRED_RECOVERY_CODE
.getCode().equals(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_METHOD_NOT_ACCEPTED_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.NOT_ACCEPTABLE);
} else {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
} catch (IdentityRecoveryException e) {
throw RecoveryUtil.handleException(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Expand Down Expand Up @@ -182,10 +265,35 @@ public Response resetPassword(ResetRequest resetRequest) {
RecoveryUtil.buildPropertiesMap(resetRequest.getProperties()));
return Response.ok().entity(buildPasswordResetResponse(successfulPasswordResetDTO)).build();
} catch (IdentityRecoveryClientException e) {
// Send the reset code again for a retry attempt.
throw RecoveryUtil.handleClientException(e, tenantDomain,
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO, resetRequest.getResetCode(),
Util.getCorrelation());
if (StringUtils.isEmpty(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, e.getErrorCode(), Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
String errorCode = RecoveryUtil.prependOperationScenarioToErrorCode(e.getErrorCode(),
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO);
if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_USER_TENANT_DOMAIN_MISS_MATCH_WITH_CONTEXT.getCode().
equals(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
} else if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_NO_ACCOUNT_RECOVERY_DATA.getCode().equals(
e.getErrorCode())) {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_NOT_FOUND_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.NOT_FOUND);
} else if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_INVALID_RECOVERY_CODE.getCode().equals(
e.getErrorCode()) || IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_EXPIRED_RECOVERY_CODE.
getCode().equals(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_METHOD_NOT_ACCEPTED_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.NOT_ACCEPTABLE);
} else if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_PASSWORD_HISTORY_VIOLATION.getCode().equals(
e.getErrorCode()) || IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_PASSWORD_POLICY_VIOLATION.
getCode().equals(e.getErrorCode())) {
// Send the reset code again for a retry attempt.
throw RecoveryUtil.buildRetryPasswordResetObject(tenantDomain, e.getMessage(), errorCode,
resetRequest.getResetCode(), Util.getCorrelation());
} else {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
} catch (IdentityRecoveryException e) {
throw RecoveryUtil.handleException(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Expand Down Expand Up @@ -220,8 +328,30 @@ public Response resendConfirmation(ResendConfirmationRequest resendConfirmationR
}
return buildResendConfirmationResponse(tenantDomain, resendConfirmationDTO);
} catch (IdentityRecoveryClientException e) {
throw RecoveryUtil.handleClientException(e, tenantDomain,
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO, Util.getCorrelation());
if (StringUtils.isEmpty(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, e.getErrorCode(), Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
String errorCode = RecoveryUtil.prependOperationScenarioToErrorCode(e.getErrorCode(),
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO);
if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_USER_TENANT_DOMAIN_MISS_MATCH_WITH_CONTEXT.getCode().
equals(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
} else if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_NO_ACCOUNT_RECOVERY_DATA.getCode().equals(
e.getErrorCode())) {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_NOT_FOUND_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.NOT_FOUND);
} else if (IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_INVALID_RECOVERY_CODE.getCode().equals(
e.getErrorCode()) || IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_INVALID_RESEND_CODE.
getCode().equals(e.getErrorCode()) || IdentityRecoveryConstants.ErrorMessages.
ERROR_CODE_EXPIRED_RECOVERY_CODE.getCode().equals(e.getErrorCode())) {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_METHOD_NOT_ACCEPTED_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.NOT_ACCEPTABLE);
} else {
throw RecoveryUtil.handleException(e, errorCode, Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
} catch (IdentityRecoveryException e) {
throw RecoveryUtil.handleException(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Expand Down
Loading

0 comments on commit 3fc7797

Please sign in to comment.