AllControllerAdvice.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.web;
import javax.validation.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.MissingRequestHeaderException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.uoc.tfg.sel.validation.NotAllowedException;
import org.uoc.tfg.sel.validation.NotFoundException;
import org.uoc.tfg.sel.validation.ValidationException;
import org.uoc.tfg.sel.web.model.ErrorCodes;
import org.uoc.tfg.sel.web.model.ErrorResponse;
/**
* Clase con los metodos generales para respuestas y procesado de los diferentes controladores
* Por ejemplo captura de errores java para respuestas concretas HTTP.
*
* @author Eduardo Rodriguez Carro
*/
@ControllerAdvice
@RestControllerAdvice
public class AllControllerAdvice {
/** The Constant logger. */
private static final Logger logger = LoggerFactory.getLogger(AllControllerAdvice.class);
/**
* Handle http message not readable exception.
*
* @param e the e
* @return the error response
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseBody
public ErrorResponse handleHttpMessageNotReadableException(final HttpMessageNotReadableException e) {
logger.error(e.getMessage(), e);
return new ErrorResponse(ErrorCodes.DATAINCONSISTENT, "I/O data inconsistent");
}
/**
* Missing Header.
*
* @param e the e
* @return the error response
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingRequestHeaderException.class)
@ResponseBody
public ErrorResponse handleMissingRequestHeaderException(final MissingRequestHeaderException e) {
logger.error(e.getMessage(), e);
return new ErrorResponse(ErrorCodes.HEADERREQUIRED, "Required header missing");
}
/**
* Sin permisos.
*
* @param e the e
* @return the error response
*/
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(BadCredentialsException.class)
@ResponseBody
public ErrorResponse handleBadCredentialsException(final BadCredentialsException e) {
logger.error(e.getMessage(), e);
return new ErrorResponse(ErrorCodes.NOTALLOWED, "Not allowed");
}
/**
* Sin permisos.
*
* @param e the e
* @return the error response
*/
@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(AccessDeniedException.class)
@ResponseBody
public ErrorResponse handleAccessDeniedException(final AccessDeniedException e) {
logger.error(e.getMessage(), e);
return new ErrorResponse(ErrorCodes.NOTALLOWED, "Not allowed");
}
/**
* Not found.
*
* @param e the e
* @return the error response
*/
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NotFoundException.class)
@ResponseBody
public ErrorResponse handleNotFoundException(final NotFoundException e) {
logger.error(e.getMessage(), e);
return e.toErrorResponse();
}
/**
* error de validacion.
*
* @param e the e
* @return the error response
*/
@ResponseStatus( HttpStatus.NOT_ACCEPTABLE)
@ExceptionHandler(NotAllowedException.class)
@ResponseBody
public ErrorResponse handleValidationException(final NotAllowedException e) {
logger.error(e.getMessage(), e);
return e.toErrorResponse();
}
/**
* error de validacion.
*
* @param e the e
* @return the error response
*/
@ResponseStatus( HttpStatus.BAD_REQUEST)
@ExceptionHandler(ValidationException.class)
@ResponseBody
public ErrorResponse handleValidationException(final ValidationException e) {
logger.error(e.getMessage(), e);
return e.toErrorResponse();
}
/**
* Handle data integrity violation exception.
*
* @param e the e
* @return the error response
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(DataIntegrityViolationException.class)
@ResponseBody
public ErrorResponse handleDataIntegrityViolationException(final DataIntegrityViolationException e) {
logger.error(e.getMessage(), e);
return new ErrorResponse(ErrorCodes.VALIDATION, "Object does not meet the storage requirements");
}
/**
* Handle bad constraint violation exception.
*
* @param e the e
* @return the error response
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class)
@ResponseBody
public ErrorResponse handleBadConstraintViolationException(final ConstraintViolationException e) {
logger.error(e.getMessage(), e);
return new ErrorResponse(ErrorCodes.VALIDATION, "Storage validation error");
}
/**
* All Other errors.
*
* @param e the e
* @return the error response
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
@ResponseBody
public ErrorResponse handleAllException(Exception e) {
logger.error(e.getMessage(), e);
return new ErrorResponse(ErrorCodes.UNKNOWN, "Generic exception");
}
}