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.web.bind.MissingServletRequestParameterException;
|
9
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
10
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
11
|
import org.springframework.web.bind.annotation.RestController;
|
12
|
|
13
|
@ControllerAdvice
|
14
|
@RestController
|
15
|
public class ExceptionsHandler {
|
16
|
private final Logger log = Logger.getLogger(this.getClass());
|
17
|
|
18
|
@ExceptionHandler(MissingServletRequestParameterException.class)
|
19
|
public ResponseEntity<ExceptionResponse> invalidInput(Exception ex) {
|
20
|
ExceptionResponse response = new ExceptionResponse();
|
21
|
response.setErrorCode("Validation Error");
|
22
|
response.setErrorMessage("Invalid inputs");
|
23
|
response.setErrors(ex.getMessage());
|
24
|
response.setStatus(HttpStatus.BAD_REQUEST);
|
25
|
log.error("invalidInput exception : "+ ex.getMessage());
|
26
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
|
27
|
}
|
28
|
|
29
|
@ExceptionHandler(NullPointerException.class)
|
30
|
public ResponseEntity<ExceptionResponse> nullPointerException(Exception ex) {
|
31
|
ExceptionResponse response = new ExceptionResponse();
|
32
|
response.setErrorCode("Null pointer Exception");
|
33
|
response.setErrorMessage("Null pointer Exception");
|
34
|
response.setErrors(ex.getMessage());
|
35
|
response.setStatus(HttpStatus.BAD_REQUEST);
|
36
|
log.error("nullPointerException exception : "+ ex.getMessage());
|
37
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
|
38
|
}
|
39
|
|
40
|
@ExceptionHandler(ChangeSetPersister.NotFoundException.class)
|
41
|
public ResponseEntity<ExceptionResponse> notFoundException(Exception ex) {
|
42
|
ExceptionResponse response = new ExceptionResponse();
|
43
|
response.setErrorCode("Not found Exception");
|
44
|
response.setErrorMessage("Not found Exception");
|
45
|
response.setErrors(ex.getMessage());
|
46
|
response.setStatus(HttpStatus.NOT_FOUND);
|
47
|
log.error("notFoundException exception : "+ ex.getMessage());
|
48
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
|
49
|
}
|
50
|
|
51
|
@ExceptionHandler(EntityNotFoundException.class)
|
52
|
public ResponseEntity<ExceptionResponse> entityNotFoundException(Exception ex) {
|
53
|
ExceptionResponse response = new ExceptionResponse();
|
54
|
response.setErrorCode("Not found Exception");
|
55
|
response.setErrorMessage("Entity not found Exception");
|
56
|
response.setErrors(ex.getMessage());
|
57
|
response.setStatus(HttpStatus.NOT_FOUND);
|
58
|
log.error("entityNotFoundException exception : "+ ex.getMessage());
|
59
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
|
60
|
}
|
61
|
|
62
|
@ExceptionHandler(PathNotValidException.class)
|
63
|
public ResponseEntity<ExceptionResponse> pathNotValidException(Exception ex) {
|
64
|
ExceptionResponse response = new ExceptionResponse();
|
65
|
response.setErrorCode("Not found Exception");
|
66
|
response.setErrorMessage("Path not valid Exception");
|
67
|
response.setErrors(ex.getMessage());
|
68
|
response.setStatus(HttpStatus.NOT_FOUND);
|
69
|
log.error("pathNotValidException exception : "+ ex.getMessage());
|
70
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
|
71
|
}
|
72
|
}
|