View Javadoc
1   package org.imageconverter.infra;
2   
3   import static org.apache.commons.lang3.StringUtils.substringAfterLast;
4   
5   import java.util.ArrayList;
6   import java.util.HashMap;
7   import java.util.List;
8   import java.util.Map;
9   
10  import javax.validation.ConstraintViolationException;
11  
12  import org.apache.commons.lang3.RegExUtils;
13  import org.imageconverter.infra.exception.BaseApplicationException;
14  import org.imageconverter.infra.exception.CsvFileGenerationException;
15  import org.imageconverter.infra.exception.CsvFileNoDataException;
16  import org.imageconverter.infra.exception.ElementConflictException;
17  import org.imageconverter.infra.exception.ElementInvalidException;
18  import org.imageconverter.infra.exception.ElementNotFoundException;
19  import org.imageconverter.infra.exception.ImageConvertServiceException;
20  import org.imageconverter.infra.exception.ServiceUnavailableException;
21  import org.springframework.context.MessageSource;
22  import org.springframework.http.HttpStatus;
23  import org.springframework.http.ResponseEntity;
24  import org.springframework.web.bind.annotation.ControllerAdvice;
25  import org.springframework.web.bind.annotation.ExceptionHandler;
26  import org.springframework.web.context.request.WebRequest;
27  
28  /**
29   * This class handle exceptions and create response properly.
30   * 
31   * @author Fernando Romulo da Silva
32   */
33  @ControllerAdvice
34  public class RestExceptionHandler extends AbstractRestExceptionHandler {
35  
36      /**
37       * Default constructor.
38       * 
39       * @param messageSource Object used to translate messages
40       */
41      protected RestExceptionHandler(final MessageSource messageSource) {
42  	super(messageSource);
43      }
44  
45      @ExceptionHandler(ElementNotFoundException.class)
46      ResponseEntity<Object> handleElementNotFoundException(final ElementNotFoundException ex, final WebRequest request) {
47  
48  	return handleObjectException(ex, request, HttpStatus.NOT_FOUND);
49      }
50  
51      @ExceptionHandler(ElementConflictException.class)
52      ResponseEntity<Object> handleElementElementConflictException(final ElementConflictException ex, final WebRequest request) {
53  
54  	return handleObjectException(ex, request, HttpStatus.CONFLICT);
55      }
56  
57      @ExceptionHandler(ElementInvalidException.class)
58      ResponseEntity<Object> handleElementInvalidException(final ElementInvalidException ex, final WebRequest request) {
59  
60  	return handleObjectException(ex, request, HttpStatus.BAD_REQUEST);
61      }
62  
63      @ExceptionHandler(ServiceUnavailableException.class)
64      ResponseEntity<Object> handleServiceUnavailableException(final ServiceUnavailableException ex, final WebRequest request) {
65  
66  	return handleObjectException(ex, request, HttpStatus.SERVICE_UNAVAILABLE);
67      }
68  
69      @ExceptionHandler(ImageConvertServiceException.class)
70      ResponseEntity<Object> handleImageConvertException(final ImageConvertServiceException ex, final WebRequest request) {
71  
72  	return handleObjectException(ex, request, HttpStatus.INTERNAL_SERVER_ERROR);
73      }
74      
75      @ExceptionHandler(CsvFileGenerationException.class)
76      ResponseEntity<Object> handleCsvFileGenerationException(final CsvFileGenerationException ex, final WebRequest request) {
77  
78  	return handleObjectException(ex, request, HttpStatus.INTERNAL_SERVER_ERROR);
79      }
80      
81      @ExceptionHandler(CsvFileNoDataException.class)
82      ResponseEntity<Object> handleCsvFileNoDataException(final CsvFileNoDataException ex, final WebRequest request) {
83  	return handleObjectException(ex, request, HttpStatus.NOT_FOUND);
84      }
85      
86      
87      @ExceptionHandler(BaseApplicationException.class)
88      ResponseEntity<Object> handleBaseApplicationException(final BaseApplicationException ex, final WebRequest request) {
89  	return handleObjectException(ex, request, HttpStatus.INTERNAL_SERVER_ERROR);
90      }
91  
92      @ExceptionHandler(ConstraintViolationException.class)
93      ResponseEntity<Object> handleConstraintViolationException(final ConstraintViolationException ex, final WebRequest request) {
94  
95  	final var subErrors = new ArrayList<Map<String, String>>();
96  
97  	final var locale = request.getLocale();
98  
99  	for (final var violation : ex.getConstraintViolations()) {
100 	    final var errors = new HashMap<String, String>();
101 
102 	    errors.put("field", substringAfterLast(violation.getPropertyPath().toString(), "."));
103 	    errors.put("value", violation.getInvalidValue().toString());
104 
105 	    final var code = RegExUtils.replaceAll(violation.getMessage(), "[{}]", "");
106 	    errors.put("error", messageSource.getMessage(code, null, locale)); // violation.getMessage());
107 
108 	    subErrors.add(errors);
109 	}
110 
111 	final var msg = messageSource.getMessage("exception.handleConstraintViolation", null, locale);
112 
113 	return handleObjectException(msg, subErrors, ex, request, HttpStatus.BAD_REQUEST);
114     }
115 
116     @ExceptionHandler(Throwable.class) // HttpMessageNotReadableException
117     ResponseEntity<Object> handleUnknownException(final Throwable ex, final WebRequest request) {
118 
119 	final var locale = request.getLocale();
120 
121 	final var msg = messageSource.getMessage("exception.handleUnknown", null, locale);
122 
123 	return handleObjectException(msg, List.of(), ex, request, HttpStatus.INTERNAL_SERVER_ERROR);
124     }
125 }