package <%=packageName%>.web.rest.errors;

import java.util.List;

import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
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.*;

/**
 * Controller advice to translate the server side exceptions to client-friendly json structures.
 */
@ControllerAdvice
public class ExceptionTranslator {

    @ExceptionHandler(ConcurrencyFailureException.class)
    @ResponseStatus(HttpStatus.CONFLICT)
    @ResponseBody
    public ErrorDTO processConcurencyError(ConcurrencyFailureException ex) {
        return new ErrorDTO(ErrorConstants.ERR_CONCURRENCY_FAILURE);
    }

    @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());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ErrorDTO processRuntimeException(Exception ex) {
        return new ErrorDTO(ErrorConstants.ERR_INTERNAL_SERVER_ERROR, "Internal server error");
    }
}

@@ -60,16 +60,76 @@
 .List;%0A%0A
+import org.springframework.core.annotation.AnnotationUtils;%0A
 import o
@@ -224,16 +224,124 @@
 Status;%0A
+import org.springframework.http.ResponseEntity;%0Aimport org.springframework.http.ResponseEntity.BodyBuilder;%0A
 import o
@@ -2752,150 +2752,592 @@
     
-@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)%0A    @ResponseBody%0A    public ErrorDTO processRuntimeException(Exception ex) %7B%0A        return
+public ResponseEntity%3CErrorDTO%3E processRuntimeException(Exception ex) throws Exception %7B%0A        BodyBuilder builder;%0A        ErrorDTO errorDTO;%0A        ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class);%0A        if (responseStatus != null) %7B%0A            builder = ResponseEntity.status(responseStatus.value());%0A            errorDTO = new ErrorDTO(%22error.%22 + responseStatus.value().value(), responseStatus.reason());%0A        %7D else %7B%0A            builder = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR);%0A            errorDTO =
  new
@@ -3414,16 +3414,65 @@
 rror%22);%0A
+        %7D%0A        return builder.body(errorDTO);%0A
     %7D%0A%7D%0A

handle exceptions annotated by ResponseStatus