All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sflpro.identity.api.common.dtos.IdentityApiError Maven / Gradle / Ivy

package com.sflpro.identity.api.common.dtos;

import com.sflpro.identity.api.common.dtos.auth.AuthenticationExceptionDto;
import com.sflpro.identity.api.common.dtos.identity.InactiveIdentityExceptionDtoDto;
import com.sflpro.identity.api.common.dtos.principal.PrincipalNameBusyExceptionDto;

import javax.ws.rs.core.Response;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Company: SFL LLC
 * Created on 3/5/17
 *
 * @author Yervand Aghababyan
 */
public enum IdentityApiError implements ApiError {
    // General
    SUCCESS(10200L, Response.Status.OK),
    NOT_FOUND(10404L, Response.Status.NOT_FOUND, "Referenced resource was not found", ResourceNotFoundDto.class),
    UNEXPECTED_INTERNAL_ERROR(10500L, Response.Status.INTERNAL_SERVER_ERROR),
    INVALID_REQUEST_DATA(11001L, Response.Status.NOT_ACCEPTABLE, "Invalid request"),

    // User and registration related errors
    IDENTITY_NOT_FOUND(10000,Response.Status.NOT_FOUND, "The specified identity was not found"),
    LOGIN_FAILED(10001, Response.Status.UNAUTHORIZED, "Incorrect principal or secret", AuthenticationExceptionDto.class),
    INACTIVE_IDENTITY(10002, Response.Status.BAD_REQUEST, "The specified identity is not active", InactiveIdentityExceptionDtoDto.class),
    PRINCIPAL_NAME_BUSY(10003, Response.Status.BAD_REQUEST, "Principal with specific type and name already exists.", PrincipalNameBusyExceptionDto.class),
    TOKEN_EXPIRED(10004, Response.Status.UNAUTHORIZED, "Token expired", AuthenticationExceptionDto.class),
    AUTH_LIMIT_REACHED(10005, Response.Status.TOO_MANY_REQUESTS, "Authentication attempts limit reached", AuthenticationExceptionDto.class),

    // Errors generated by the client library
    FAILED_TO_PARSE_RESPONSE(12001L, Response.Status.INTERNAL_SERVER_ERROR, "Failed to parse the response sent by the server"),
    UNKNOWN_CLIENT_ERROR(12002L, Response.Status.INTERNAL_SERVER_ERROR, "Unknown error while processing server's response");

    private static final Map> exceptionClasses;
    private static volatile boolean initializationComplete;

    static {

        Map> exceptionClassesTmp = new HashMap<>(IdentityApiError.values().length);
        for (IdentityApiError error : IdentityApiError.values()) {
            exceptionClassesTmp.put(error.getErrorCode(), error.getExceptionClass());
        }

        exceptionClasses = Collections.unmodifiableMap(exceptionClassesTmp);
        initializationComplete = true;
    }

    private final long errorCode;
    private final Response.Status responseHttpStatus;
    private final String defaultMessage;
    private final Class exceptionClass;

    IdentityApiError(long errorCode, Response.Status responseHttpStatus, String defaultMessage, Class clazz) {
        this.errorCode = errorCode;
        this.responseHttpStatus = responseHttpStatus;
        this.defaultMessage = defaultMessage;
        this.exceptionClass = clazz;
    }

    IdentityApiError(long errorCode, Response.Status responseHttpStatus, String defaultMessage) {
        this(errorCode, responseHttpStatus, defaultMessage, null);

    }

    IdentityApiError(long errorCode, Response.Status responseHttpStatus) {
        this(errorCode, responseHttpStatus, null);
    }

    IdentityApiError(IdentityApiError genericError) {
        this(
                genericError.getErrorCode(),
                genericError.getResponseHttpStatus(),
                genericError.getDefaultMessage(),
                genericError.getExceptionClass());
    }

    @Override
    public long getErrorCode() {
        return errorCode;
    }

    @Override
    public Response.Status getResponseHttpStatus() {
        return responseHttpStatus;
    }

    @Override
    public String getDefaultMessage() {
        return defaultMessage;
    }

    @Override
    public Class getExceptionClass() {
        return exceptionClass;
    }

    public static Class getExceptionClass(long errorCode) {
        if (!initializationComplete) {
            throw new IllegalStateException("Initialization of the exception mapping hasn't yet completed!");
        }
        return exceptionClasses.get(errorCode);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy