1
|
package eu.dnetlib.uoaadmintoolslibrary.handlers;
|
2
|
|
3
|
import eu.dnetlib.uoaadmintoolslibrary.responses.ExceptionResponse;
|
4
|
import org.apache.log4j.Logger;
|
5
|
import org.springframework.dao.DuplicateKeyException;
|
6
|
import org.springframework.data.crossstore.ChangeSetPersister;
|
7
|
import org.springframework.http.HttpStatus;
|
8
|
import org.springframework.http.ResponseEntity;
|
9
|
import org.springframework.web.bind.MissingServletRequestParameterException;
|
10
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
11
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
12
|
|
13
|
@ControllerAdvice
|
14
|
public class AdminToolsLibraryExceptionsHandler {
|
15
|
private final Logger log = Logger.getLogger(this.getClass());
|
16
|
|
17
|
@ExceptionHandler(MissingServletRequestParameterException.class)
|
18
|
public ResponseEntity<ExceptionResponse> invalidInput(Exception ex) {
|
19
|
ExceptionResponse response = new ExceptionResponse();
|
20
|
response.setErrorCode("Validation Error");
|
21
|
response.setErrorMessage("Invalid inputs.");
|
22
|
response.setErrors(ex.getMessage());
|
23
|
log.debug("invalidInput exception");
|
24
|
|
25
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
|
26
|
}
|
27
|
|
28
|
@ExceptionHandler(MismatchingContentException.class)
|
29
|
public ResponseEntity<ExceptionResponse> mismatchingContentException(Exception ex) {
|
30
|
ExceptionResponse response = new ExceptionResponse();
|
31
|
response.setErrorCode("Conflicting content given");
|
32
|
response.setErrorMessage(ex.getMessage());
|
33
|
response.setErrors(ex.getMessage());
|
34
|
log.debug("mismatchingContent exception" + response.getErrorCode()+ " "+response.getErrorMessage());
|
35
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
|
36
|
}
|
37
|
|
38
|
@ExceptionHandler(ContentNotFoundException.class)
|
39
|
public ResponseEntity<ExceptionResponse> contentNotFoundException(Exception ex) {
|
40
|
ExceptionResponse response = new ExceptionResponse();
|
41
|
response.setErrorCode("No content found");
|
42
|
response.setErrorMessage(ex.getMessage());
|
43
|
response.setErrors(ex.getMessage());
|
44
|
log.debug("contentNotFound exception" + response.getErrorCode()+ " "+response.getErrorMessage());
|
45
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
|
46
|
}
|
47
|
|
48
|
@ExceptionHandler(NullPointerException.class)
|
49
|
public ResponseEntity<ExceptionResponse> nullPointerException(Exception ex) {
|
50
|
ExceptionResponse response = new ExceptionResponse();
|
51
|
response.setErrorCode("Null pointer Exception");
|
52
|
response.setErrorMessage("Null pointer Exception");
|
53
|
response.setErrors(ex.getMessage());
|
54
|
log.debug("nullPointerException exception");
|
55
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
|
56
|
}
|
57
|
|
58
|
@ExceptionHandler(InvalidReCaptchaException.class)
|
59
|
public ResponseEntity<ExceptionResponse> invalidReCaptchaException(Exception ex) {
|
60
|
ExceptionResponse response = new ExceptionResponse();
|
61
|
response.setErrorCode("Invalid ReCaptcha Exception");
|
62
|
response.setErrorMessage("Invalid ReCaptcha Exception");
|
63
|
response.setErrors(ex.getMessage());
|
64
|
log.debug("invalidReCaptchaException exception");
|
65
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
|
66
|
}
|
67
|
|
68
|
|
69
|
@ExceptionHandler(ChangeSetPersister.NotFoundException.class)
|
70
|
public ResponseEntity<ExceptionResponse> notFoundException(Exception ex) {
|
71
|
ExceptionResponse response = new ExceptionResponse();
|
72
|
response.setErrorCode("Not found Exception");
|
73
|
response.setErrorMessage("Not found Exception");
|
74
|
response.setErrors(ex.getMessage());
|
75
|
response.setStatus(HttpStatus.NOT_FOUND);
|
76
|
log.error("notFoundException exception : "+ ex.getMessage());
|
77
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
|
78
|
}
|
79
|
|
80
|
@ExceptionHandler(ForbiddenException.class)
|
81
|
public ResponseEntity<ExceptionResponse> forbiddenException(Exception ex) {
|
82
|
ExceptionResponse response = new ExceptionResponse();
|
83
|
response.setErrorCode("Forbidden Exception");
|
84
|
response.setErrorMessage("Forbidden Exception");
|
85
|
response.setErrors(ex.getMessage());
|
86
|
response.setStatus(HttpStatus.FORBIDDEN);
|
87
|
log.error("forbiddenException exception : "+ ex.getMessage());
|
88
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.FORBIDDEN);
|
89
|
}
|
90
|
|
91
|
@ExceptionHandler(DuplicateKeyException.class)
|
92
|
public ResponseEntity<ExceptionResponse> duplicateKeyException(Exception ex) {
|
93
|
ExceptionResponse response = new ExceptionResponse();
|
94
|
response.setErrorCode("DuplicateKey Exception");
|
95
|
response.setErrorMessage("DuplicateKey Exception");
|
96
|
response.setErrors(ex.getMessage());
|
97
|
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
|
98
|
log.error("duplicateKeyException exception : "+ ex.getMessage());
|
99
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.INTERNAL_SERVER_ERROR);
|
100
|
}
|
101
|
}
|