2019-11-04 10:03:49 +01:00
|
|
|
package eu.dnetlib.uoamonitorservice.handlers;
|
|
|
|
|
|
|
|
import eu.dnetlib.uoamonitorservice.responses.ExceptionResponse;
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
import org.springframework.data.crossstore.ChangeSetPersister;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
2020-11-11 11:52:38 +01:00
|
|
|
import org.springframework.security.access.AccessDeniedException;
|
2019-11-04 10:03:49 +01:00
|
|
|
import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
2020-11-11 11:52:38 +01:00
|
|
|
import org.springframework.web.multipart.support.MissingServletRequestPartException;
|
2019-11-04 10:03:49 +01:00
|
|
|
|
|
|
|
@ControllerAdvice
|
|
|
|
@RestController
|
|
|
|
public class ExceptionsHandler {
|
|
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
|
|
|
|
|
|
|
@ExceptionHandler(MissingServletRequestParameterException.class)
|
|
|
|
public ResponseEntity<ExceptionResponse> invalidInput(Exception ex) {
|
|
|
|
ExceptionResponse response = new ExceptionResponse();
|
|
|
|
response.setErrorCode("Validation Error");
|
2019-11-22 16:50:59 +01:00
|
|
|
response.setErrorMessage("Invalid inputs");
|
2019-11-04 10:03:49 +01:00
|
|
|
response.setErrors(ex.getMessage());
|
|
|
|
response.setStatus(HttpStatus.BAD_REQUEST);
|
2020-06-17 13:37:42 +02:00
|
|
|
log.error("invalidInput exception : "+ ex.getMessage());
|
2019-11-04 10:03:49 +01:00
|
|
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
@ExceptionHandler(NullPointerException.class)
|
|
|
|
public ResponseEntity<ExceptionResponse> nullPointerException(Exception ex) {
|
|
|
|
ExceptionResponse response = new ExceptionResponse();
|
|
|
|
response.setErrorCode("Null pointer Exception");
|
|
|
|
response.setErrorMessage("Null pointer Exception");
|
|
|
|
response.setErrors(ex.getMessage());
|
|
|
|
response.setStatus(HttpStatus.BAD_REQUEST);
|
2020-06-17 13:37:42 +02:00
|
|
|
log.error("nullPointerException exception : "+ ex.getMessage());
|
2019-11-04 10:03:49 +01:00
|
|
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
@ExceptionHandler(ChangeSetPersister.NotFoundException.class)
|
|
|
|
public ResponseEntity<ExceptionResponse> notFoundException(Exception ex) {
|
|
|
|
ExceptionResponse response = new ExceptionResponse();
|
|
|
|
response.setErrorCode("Not found Exception");
|
|
|
|
response.setErrorMessage("Not found Exception");
|
|
|
|
response.setErrors(ex.getMessage());
|
|
|
|
response.setStatus(HttpStatus.NOT_FOUND);
|
2020-06-17 13:37:42 +02:00
|
|
|
log.error("notFoundException exception : "+ ex.getMessage());
|
2019-11-04 10:03:49 +01:00
|
|
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
|
|
|
|
}
|
2019-11-22 16:50:59 +01:00
|
|
|
|
|
|
|
@ExceptionHandler(EntityNotFoundException.class)
|
|
|
|
public ResponseEntity<ExceptionResponse> entityNotFoundException(Exception ex) {
|
|
|
|
ExceptionResponse response = new ExceptionResponse();
|
|
|
|
response.setErrorCode("Not found Exception");
|
|
|
|
response.setErrorMessage("Entity not found Exception");
|
|
|
|
response.setErrors(ex.getMessage());
|
|
|
|
response.setStatus(HttpStatus.NOT_FOUND);
|
2020-06-17 13:37:42 +02:00
|
|
|
log.error("entityNotFoundException exception : "+ ex.getMessage());
|
2019-11-22 16:50:59 +01:00
|
|
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
@ExceptionHandler(PathNotValidException.class)
|
|
|
|
public ResponseEntity<ExceptionResponse> pathNotValidException(Exception ex) {
|
|
|
|
ExceptionResponse response = new ExceptionResponse();
|
|
|
|
response.setErrorCode("Not found Exception");
|
|
|
|
response.setErrorMessage("Path not valid Exception");
|
|
|
|
response.setErrors(ex.getMessage());
|
|
|
|
response.setStatus(HttpStatus.NOT_FOUND);
|
2020-06-17 13:37:42 +02:00
|
|
|
log.error("pathNotValidException exception : "+ ex.getMessage());
|
2019-11-22 16:50:59 +01:00
|
|
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
|
|
|
|
}
|
2020-11-11 11:52:38 +01:00
|
|
|
|
|
|
|
@ExceptionHandler(AccessDeniedException.class)
|
|
|
|
public ResponseEntity<ExceptionResponse> accessDeniedException(Exception ex) {
|
|
|
|
ExceptionResponse response = new ExceptionResponse();
|
|
|
|
response.setErrorCode("Forbidden Exception");
|
|
|
|
response.setErrorMessage("Access Denied Exception");
|
|
|
|
response.setErrors(ex.getMessage());
|
|
|
|
response.setStatus(HttpStatus.FORBIDDEN);
|
|
|
|
log.error("accessDeniedException exception : "+ ex.getMessage());
|
|
|
|
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.FORBIDDEN);
|
|
|
|
}
|
2019-11-04 10:03:49 +01:00
|
|
|
}
|