-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
65 lines (61 loc) · 3.33 KB
/
GlobalExceptionHandler.java
File metadata and controls
65 lines (61 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.ecommerece.productservice.exceptions;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.hibernate.exception.DataException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<Map<String, Object>> handlerResourceNotFoundException(ResourceNotFoundException e) {
Map<String, Object> map = getExceptionDetailsMap(e.getMessage(),HttpStatus.NOT_FOUND);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(map);
}
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<Map<String, Object>> illegalArguments(IllegalArgumentException e){
Map<String, Object> map = getExceptionDetailsMap(e.getMessage(),HttpStatus.BAD_REQUEST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(map);
}
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<Map<String, Object>> invalidMethodArgumentTypeException(MethodArgumentTypeMismatchException e){
Map<String, Object> map = getExceptionDetailsMap(e.getMessage(), HttpStatus.BAD_REQUEST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(map);
}
@ExceptionHandler(InvalidFormatException.class)
public ResponseEntity<Map<String, Object>> invalidFormatException(InvalidFormatException e){
Map<String, Object> map = getExceptionDetailsMap(e.getOriginalMessage(),HttpStatus.BAD_REQUEST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(map);
}
@ExceptionHandler(DataException.class)
public ResponseEntity<Map<String, Object>> illegalDataException(DataException e){
Map<String, Object> map = getExceptionDetailsMap(e.getMessage(), HttpStatus.BAD_REQUEST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(map);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, List<String>>> handleValidationErrors(MethodArgumentNotValidException ex) {
List<String> errors = ex.getBindingResult().getFieldErrors()
.stream().map(FieldError::getDefaultMessage).collect(Collectors.toList());
return new ResponseEntity<>(getErrorsMap(errors), new HttpHeaders(), HttpStatus.BAD_REQUEST);
}
private Map<String, List<String>> getErrorsMap(List<String> errors) {
Map<String, List<String>> errorResponse = new HashMap<>();
errorResponse.put("errors", errors);
return errorResponse;
}
private static Map<String, Object> getExceptionDetailsMap(String msg, HttpStatus status) {
Map<String, Object> map = new HashMap<>();
map.put("message", msg);
map.put("success", false);
map.put("status", status);
return map;
}
}