Project

General

Profile

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.debug("invalidInput exception");
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.debug("nullPointerException exception");
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.debug("notFoundException exception");
48
        return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
49
    }
50
}
(2-2/2)