Project

General

Profile

1
package eu.dnetlib.uoaadmintools.handlers;
2

    
3
import eu.dnetlib.uoaadmintools.responses.ExceptionResponse;
4

    
5
import eu.dnetlib.uoaadmintoolslibrary.handlers.InvalidReCaptchaException;
6
import org.apache.log4j.Logger;
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
import java.util.Date;
14

    
15
@ControllerAdvice
16
public class ExceptionsHandler {
17
    private final Logger log = Logger.getLogger(this.getClass());
18

    
19
    @ExceptionHandler(MissingServletRequestParameterException.class)
20
    public ResponseEntity<ExceptionResponse> invalidInput(Exception ex) {
21
        ExceptionResponse response = new ExceptionResponse();
22
        response.setErrorCode("Validation Error");
23
        response.setErrorMessage("Invalid inputs.");
24
        response.setErrors(ex.getMessage());
25
        log.debug("invalidInput exception");
26

    
27
        return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
28
    }
29
    @ExceptionHandler(ContentNotFoundException.class)
30
    public ResponseEntity<ExceptionResponse> contentNotFound(Exception ex) {
31
        ExceptionResponse response = new ExceptionResponse();
32
        response.setErrorCode("No content found");
33
        response.setErrorMessage(ex.getMessage());
34
        response.setErrors(ex.getMessage());
35
        log.debug("contentNotFound exception" + response.getErrorCode()+ " "+response.getErrorMessage());
36
        return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
37
    }
38
    @ExceptionHandler(NullPointerException.class)
39
    public ResponseEntity<ExceptionResponse> nullPointerException(Exception ex) {
40
        ExceptionResponse response = new ExceptionResponse();
41
        response.setErrorCode("Null pointer Exception");
42
        response.setErrorMessage("Null pointer Exception");
43
        response.setErrors(ex.getMessage());
44
        log.debug("nullPointerException exception");
45
        return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
46
    }
47

    
48
    @ExceptionHandler(InvalidReCaptchaException.class)
49
    public ResponseEntity<ExceptionResponse> invalidReCaptchaException(Exception ex) {
50
        ExceptionResponse response = new ExceptionResponse();
51
        response.setErrorCode("Invalid ReCaptcha Exception");
52
        response.setErrorMessage("Invalid ReCaptcha Exception");
53
        response.setErrors(ex.getMessage());
54
        log.debug("invalidReCaptchaException exception");
55
        return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
56
    }
57
}
(5-5/5)