ValidationException.java

/**
 * TFG 75.678 - TFG Desarrollo web 2020 e-Learning for Schools
 * Copyright (C) 2020  Eduardo Rodriguez Carro
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.uoc.tfg.sel.validation;

import org.uoc.tfg.sel.web.model.ErrorCodes;
import org.uoc.tfg.sel.web.model.ErrorResponse;

/**
 * Exception de validacion
 * Tipo runtime, se lanza y es capturada por el controller advice para procesar un tipo de error de salida generico
 * Al ser runtime el controlador de transacciones la trata como una excepcion que debe generar un rollback.
 *
 * @author Eduardo Rodriguez Carro
 */
public class ValidationException extends RuntimeException {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = -2416548401520450450L;

    /** The error code. */
    private final ErrorCodes errorCode;

    /**
     * Throw it.
     *
     * @param errorCode the error code
     * @param message the message
     */
    public static void throwIt(ErrorCodes errorCode, String message ) {
    	throw new ValidationException(errorCode,message);
    }
    
    /**
     * Throw it.
     *
     * @param message the message
     */
    public static void throwIt(String message ) {
    	throw new ValidationException(ErrorCodes.UNKNOWN,message);
    }
    
    /**
     * Instantiates a new validation exception.
     *
     * @param message the message
     */
    public ValidationException(String message) {
        super(message);
        errorCode = ErrorCodes.UNKNOWN;
    }


    /**
     * Instantiates a new validation exception.
     *
     * @param message the message
     * @param e the e
     */
    public ValidationException(String message, Throwable e) {
        super(message, e);
        errorCode = ErrorCodes.UNKNOWN;
    }


    /**
     * Instantiates a new validation exception.
     *
     * @param errorCode the error code
     * @param message the message
     */
    public ValidationException(ErrorCodes errorCode, String message) {
        super(message);
        this.errorCode = errorCode;
    }


    /**
     * To error response.
     *
     * @return the error response
     */
    public ErrorResponse toErrorResponse() {
        return new ErrorResponse(errorCode, this.getMessage());
    }

}