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 26, 2023
1 parent 8a5d4f0 commit 089d7d6
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 302 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@

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;
import org.wso2.carbon.identity.recovery.IdentityRecoveryException;
import org.wso2.carbon.identity.recovery.dto.PasswordRecoverDTO;
Expand Down Expand Up @@ -56,7 +53,6 @@
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 @@ -87,51 +83,9 @@ public Response initiatePasswordRecovery(InitRequest initRequest) {
RecoveryUtil.buildPropertiesMap(initRequest.getProperties()));
return Response.ok().entity(buildPasswordRecoveryInitResponse(tenantDomain, recoveryInformationDTO))
.build();
} catch (IdentityRecoveryClientException e) {
if (StringUtils.isEmpty(e.getErrorCode())) {
throw RecoveryUtil.handleInternalServerError(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Constants.STATUS_INTERNAL_SERVER_ERROR_DESCRIPTION_DEFAULT,
Response.Status.INTERNAL_SERVER_ERROR);
}
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(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(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(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(errorCode, Constants.STATUS_NOT_FOUND_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.NOT_FOUND);
} else {
throw RecoveryUtil.handleException(errorCode, Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
} catch (IdentityRecoveryException e) {
throw RecoveryUtil.handleInternalServerError(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Constants.STATUS_INTERNAL_SERVER_ERROR_DESCRIPTION_DEFAULT, Response.Status.INTERNAL_SERVER_ERROR);
throw RecoveryUtil.handleIdentityRecoveryException(e, tenantDomain,
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO, Util.getCorrelation());
}
}

Expand Down Expand Up @@ -164,38 +118,9 @@ public Response recoverPassword(RecoveryRequest recoveryRequest) {
}
return buildPasswordRecoveryResponse(tenantDomain, passwordRecoverDTO.getNotificationChannel(),
passwordRecoverDTO);
} catch (IdentityRecoveryClientException e) {
if (StringUtils.isEmpty(e.getErrorCode())) {
throw RecoveryUtil.handleInternalServerError(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Constants.STATUS_INTERNAL_SERVER_ERROR_DESCRIPTION_DEFAULT,
Response.Status.INTERNAL_SERVER_ERROR);
}
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(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(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(errorCode, Constants.STATUS_METHOD_NOT_ACCEPTED_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.NOT_ACCEPTABLE);
} else {
throw RecoveryUtil.handleException(errorCode, Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
} catch (IdentityRecoveryException e) {
throw RecoveryUtil.handleInternalServerError(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Constants.STATUS_INTERNAL_SERVER_ERROR_DESCRIPTION_DEFAULT, Response.Status.INTERNAL_SERVER_ERROR);
throw RecoveryUtil.handleIdentityRecoveryException(e, tenantDomain,
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO, Util.getCorrelation());
}
}

Expand All @@ -217,36 +142,9 @@ public Response confirmRecovery(ConfirmRequest confirmRequest) {
.confirm(confirmRequest.getOtp(), confirmRequest.getConfirmationCode(), tenantDomain,
RecoveryUtil.buildPropertiesMap(confirmRequest.getProperties()));
return Response.ok().entity(buildResetCodeResponse(tenantDomain, passwordResetCodeDTO)).build();
} catch (IdentityRecoveryClientException e) {
if (StringUtils.isEmpty(e.getErrorCode())) {
throw RecoveryUtil.handleInternalServerError(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Constants.STATUS_INTERNAL_SERVER_ERROR_DESCRIPTION_DEFAULT,
Response.Status.INTERNAL_SERVER_ERROR);
}
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(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(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(errorCode, Constants.STATUS_METHOD_NOT_ACCEPTED_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.NOT_ACCEPTABLE);
} else {
throw RecoveryUtil.handleException(errorCode, Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
} catch (IdentityRecoveryException e) {
throw RecoveryUtil.handleInternalServerError(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Constants.STATUS_INTERNAL_SERVER_ERROR_DESCRIPTION_DEFAULT, Response.Status.INTERNAL_SERVER_ERROR);
throw RecoveryUtil.handleIdentityRecoveryException(e, tenantDomain,
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO, Util.getCorrelation());
}
}

Expand All @@ -270,42 +168,11 @@ public Response resetPassword(ResetRequest resetRequest) {
.reset(resetRequest.getResetCode(), resetRequest.getFlowConfirmationCode(), password,
RecoveryUtil.buildPropertiesMap(resetRequest.getProperties()));
return Response.ok().entity(buildPasswordResetResponse(successfulPasswordResetDTO)).build();
} catch (IdentityRecoveryClientException e) {
if (StringUtils.isEmpty(e.getErrorCode())) {
throw RecoveryUtil.handleInternalServerError(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Constants.STATUS_INTERNAL_SERVER_ERROR_DESCRIPTION_DEFAULT,
Response.Status.INTERNAL_SERVER_ERROR);
}
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(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(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(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(errorCode, Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
} catch (IdentityRecoveryException e) {
throw RecoveryUtil.handleInternalServerError(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Constants.STATUS_INTERNAL_SERVER_ERROR_DESCRIPTION_DEFAULT, Response.Status.INTERNAL_SERVER_ERROR);
// Send the reset code again for a retry attempt.
throw RecoveryUtil.handleIdentityRecoveryException(e, tenantDomain,
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO, resetRequest.getResetCode(),
Util.getCorrelation());
}
}

Expand Down Expand Up @@ -335,37 +202,9 @@ public Response resendConfirmation(ResendConfirmationRequest resendConfirmationR
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
return buildResendConfirmationResponse(tenantDomain, resendConfirmationDTO);
} catch (IdentityRecoveryClientException e) {
if (StringUtils.isEmpty(e.getErrorCode())) {
throw RecoveryUtil.handleInternalServerError(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Constants.STATUS_INTERNAL_SERVER_ERROR_DESCRIPTION_DEFAULT,
Response.Status.INTERNAL_SERVER_ERROR);
}
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(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(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(errorCode, Constants.STATUS_METHOD_NOT_ACCEPTED_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.NOT_ACCEPTABLE);
} else {
throw RecoveryUtil.handleException(errorCode, Constants.STATUS_CONFLICT_MESSAGE_DEFAULT,
e.getMessage(), Response.Status.CONFLICT);
}
} catch (IdentityRecoveryException e) {
throw RecoveryUtil.handleInternalServerError(e, e.getErrorCode(),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT,
Constants.STATUS_INTERNAL_SERVER_ERROR_DESCRIPTION_DEFAULT, Response.Status.INTERNAL_SERVER_ERROR);
throw RecoveryUtil.handleIdentityRecoveryException(e, tenantDomain,
IdentityRecoveryConstants.PASSWORD_RECOVERY_SCENARIO, Util.getCorrelation());
}
}

Expand Down
Loading

0 comments on commit 089d7d6

Please sign in to comment.