1
|
package eu.dnetlib.uoamonitorservice.handlers;
|
2
|
|
3
|
import eu.dnetlib.uoamonitorservice.responses.ExceptionResponse;
|
4
|
import org.apache.log4j.Logger;
|
5
|
import org.springframework.data.crossstore.ChangeSetPersister;
|
6
|
import org.springframework.http.HttpStatus;
|
7
|
import org.springframework.http.ResponseEntity;
|
8
|
import org.springframework.security.access.AccessDeniedException;
|
9
|
import org.springframework.web.bind.MissingServletRequestParameterException;
|
10
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
11
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
12
|
import org.springframework.web.bind.annotation.RestController;
|
13
|
import org.springframework.web.multipart.support.MissingServletRequestPartException;
|
14
|
|
15
|
@ControllerAdvice
|
16
|
@RestController
|
17
|
public class ExceptionsHandler {
|
18
|
private final Logger log = Logger.getLogger(this.getClass());
|
19
|
|
20
|
@ExceptionHandler(MissingServletRequestParameterException.class)
|
21
|
public ResponseEntity<ExceptionResponse> invalidInput(Exception ex) {
|
22
|
ExceptionResponse response = new ExceptionResponse();
|
23
|
response.setErrorCode("Validation Error");
|
24
|
response.setErrorMessage("Invalid inputs");
|
25
|
response.setErrors(ex.getMessage());
|
26
|
response.setStatus(HttpStatus.BAD_REQUEST);
|
27
|
log.error("invalidInput exception : "+ ex.getMessage());
|
28
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
|
29
|
}
|
30
|
|
31
|
@ExceptionHandler(NullPointerException.class)
|
32
|
public ResponseEntity<ExceptionResponse> nullPointerException(Exception ex) {
|
33
|
ExceptionResponse response = new ExceptionResponse();
|
34
|
response.setErrorCode("Null pointer Exception");
|
35
|
response.setErrorMessage("Null pointer Exception");
|
36
|
response.setErrors(ex.getMessage());
|
37
|
response.setStatus(HttpStatus.BAD_REQUEST);
|
38
|
log.error("nullPointerException exception : "+ ex.getMessage());
|
39
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
|
40
|
}
|
41
|
|
42
|
@ExceptionHandler(ChangeSetPersister.NotFoundException.class)
|
43
|
public ResponseEntity<ExceptionResponse> notFoundException(Exception ex) {
|
44
|
ExceptionResponse response = new ExceptionResponse();
|
45
|
response.setErrorCode("Not found Exception");
|
46
|
response.setErrorMessage("Not found Exception");
|
47
|
response.setErrors(ex.getMessage());
|
48
|
response.setStatus(HttpStatus.NOT_FOUND);
|
49
|
log.error("notFoundException exception : "+ ex.getMessage());
|
50
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
|
51
|
}
|
52
|
|
53
|
@ExceptionHandler(EntityNotFoundException.class)
|
54
|
public ResponseEntity<ExceptionResponse> entityNotFoundException(Exception ex) {
|
55
|
ExceptionResponse response = new ExceptionResponse();
|
56
|
response.setErrorCode("Not found Exception");
|
57
|
response.setErrorMessage("Entity not found Exception");
|
58
|
response.setErrors(ex.getMessage());
|
59
|
response.setStatus(HttpStatus.NOT_FOUND);
|
60
|
log.error("entityNotFoundException exception : "+ ex.getMessage());
|
61
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
|
62
|
}
|
63
|
|
64
|
@ExceptionHandler(PathNotValidException.class)
|
65
|
public ResponseEntity<ExceptionResponse> pathNotValidException(Exception ex) {
|
66
|
ExceptionResponse response = new ExceptionResponse();
|
67
|
response.setErrorCode("Not found Exception");
|
68
|
response.setErrorMessage("Path not valid Exception");
|
69
|
response.setErrors(ex.getMessage());
|
70
|
response.setStatus(HttpStatus.NOT_FOUND);
|
71
|
log.error("pathNotValidException exception : "+ ex.getMessage());
|
72
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
|
73
|
}
|
74
|
|
75
|
@ExceptionHandler(AccessDeniedException.class)
|
76
|
public ResponseEntity<ExceptionResponse> accessDeniedException(Exception ex) {
|
77
|
ExceptionResponse response = new ExceptionResponse();
|
78
|
response.setErrorCode("Forbidden Exception");
|
79
|
response.setErrorMessage("Access Denied Exception");
|
80
|
response.setErrors(ex.getMessage());
|
81
|
response.setStatus(HttpStatus.FORBIDDEN);
|
82
|
log.error("accessDeniedException exception : "+ ex.getMessage());
|
83
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.FORBIDDEN);
|
84
|
}
|
85
|
}
|