package ee.esutoniagodesu.web.rest.errors;
import ee.esutoniagodesu.service.LocaleService;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
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 javax.inject.Inject;
import java.util.List;
/**
* Controller advice to translate the server side exceptions to client-friendly json structures.
*/
@ControllerAdvice
public class ExceptionTranslator {
@Inject
private LocaleService localeService;
@ExceptionHandler(BadCredentialsException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ResponseBody
public ErrorDTO processAuthenticationError(BadCredentialsException ex) {
return new ErrorDTO(ErrorConstants.ERR_UNAUTHORIZED);
}
@ExceptionHandler(ConcurrencyFailureException.class)
@ResponseStatus(HttpStatus.CONFLICT)
@ResponseBody
public ErrorDTO processConcurencyError(ConcurrencyFailureException ex) {
return new ErrorDTO(ErrorConstants.ERR_CONCURRENCY_FAILURE);
}
@ExceptionHandler(CustomLocalizedException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorDTO processCustomLocalizedException(CustomLocalizedException ex) {
return new ErrorDTO(ex.getCode(), localeService.m(ex.getCode()), null);
}
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorDTO processIllegalArgumentException(IllegalArgumentException ex) {
return new ErrorDTO(ex.getMessage());
}
@ExceptionHandler(IllegalStateException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public ErrorDTO processIllegalStateException(IllegalStateException ex) {
return new ErrorDTO(ex.getLocalizedMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorDTO processValidationError(MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
List<FieldError> fieldErrors = result.getFieldErrors();
return processFieldErrors(fieldErrors);
}
@ExceptionHandler(CustomParameterizedException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ParameterizedErrorDTO processParameterizedValidationError(CustomParameterizedException ex) {
return ex.getErrorDTO();
}
@ExceptionHandler(AccessDeniedException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
@ResponseBody
public ErrorDTO processAccessDeniedExcpetion(AccessDeniedException e) {
return new ErrorDTO(ErrorConstants.ERR_ACCESS_DENIED, e.getMessage());
}
private ErrorDTO processFieldErrors(List<FieldError> fieldErrors) {
ErrorDTO dto = new ErrorDTO(ErrorConstants.ERR_VALIDATION);
for (FieldError fieldError : fieldErrors) {
dto.add(fieldError.getObjectName(), fieldError.getField(), fieldError.getCode());
}
return dto;
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseBody
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public ErrorDTO processMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
return new ErrorDTO(ErrorConstants.ERR_METHOD_NOT_SUPPORTED, exception.getMessage());
}
}