Project

General

Profile

1
package eu.dnetlib.uoamonitorservice.handlers;
2

    
3
import eu.dnetlib.uoaadmintoolslibrary.responses.ExceptionResponse;
4
import org.apache.log4j.Logger;
5
import org.springframework.http.HttpStatus;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.web.bind.annotation.ControllerAdvice;
8
import org.springframework.web.bind.annotation.ExceptionHandler;
9
import org.springframework.web.bind.annotation.RestController;
10

    
11
@ControllerAdvice
12
@RestController
13
public class ExceptionsHandler {
14
    private final Logger log = Logger.getLogger(this.getClass());
15

    
16
    @ExceptionHandler(EntityNotFoundException.class)
17
    public ResponseEntity<ExceptionResponse> entityNotFoundException(Exception ex) {
18
        ExceptionResponse response = new ExceptionResponse();
19
        response.setErrorCode("Not found Exception");
20
        response.setErrorMessage("Entity not found Exception");
21
        response.setErrors(ex.getMessage());
22
        response.setStatus(HttpStatus.NOT_FOUND);
23
        log.error("entityNotFoundException exception : "+ ex.getMessage());
24
        return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
25
    }
26

    
27
    @ExceptionHandler(PathNotValidException.class)
28
    public ResponseEntity<ExceptionResponse> pathNotValidException(Exception ex) {
29
        ExceptionResponse response = new ExceptionResponse();
30
        response.setErrorCode("Not found Exception");
31
        response.setErrorMessage("Path not valid Exception");
32
        response.setErrors(ex.getMessage());
33
        response.setStatus(HttpStatus.NOT_FOUND);
34
        log.error("pathNotValidException exception : "+ ex.getMessage());
35
        return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
36
    }
37

    
38
}
(2-2/3)