All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.kgrid.shelf.controller.ShelfExceptionHandler Maven / Gradle / Ivy
package org.kgrid.shelf.controller;
import org.kgrid.shelf.ShelfException;
import org.kgrid.shelf.ShelfResourceNotFound;
import org.kgrid.shelf.repository.KnowledgeObjectRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@RestController
public abstract class ShelfExceptionHandler {
protected Logger log = LoggerFactory.getLogger(getClass().getName());
protected Optional kod;
protected KnowledgeObjectRepository koRepo;
@Autowired
public ShelfExceptionHandler(
KnowledgeObjectRepository koRepo, Optional kod) {
this.koRepo = koRepo;
this.kod = kod;
}
// Exception handling:
@ExceptionHandler(NullPointerException.class)
public ResponseEntity> handleObjectNotFoundExceptions(
NullPointerException e, WebRequest request) {
return new ResponseEntity<>(
getErrorMap(request, e.getMessage(), HttpStatus.NOT_FOUND), HttpStatus.NOT_FOUND);
}
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity> handleObjectNotFoundExceptions(
IllegalArgumentException e, WebRequest request) {
return new ResponseEntity<>(
getErrorMap(request, e.getMessage(), HttpStatus.BAD_REQUEST), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(IOException.class)
public ResponseEntity> handleObjectNotFoundExceptions(
IOException e, WebRequest request) {
return new ResponseEntity<>(
getErrorMap(request, e.getMessage(), HttpStatus.NOT_FOUND), HttpStatus.NOT_FOUND);
}
@ExceptionHandler(NoSuchFileException.class)
public ResponseEntity> handleObjectNotFoundExceptions(
NoSuchFileException e, WebRequest request) {
return new ResponseEntity<>(
getErrorMap(request, e.getMessage(), HttpStatus.NOT_FOUND), HttpStatus.NOT_FOUND);
}
@ExceptionHandler(NoSuchFieldException.class)
public ResponseEntity> handleObjectNotFoundExceptions(
NoSuchFieldException e, WebRequest request) {
return new ResponseEntity<>(
getErrorMap(request, e.getMessage(), HttpStatus.NOT_FOUND), HttpStatus.NOT_FOUND);
}
@ExceptionHandler(ShelfResourceNotFound.class)
public ResponseEntity> handleShelfResourceNotFoundExceptions(
ShelfException e, WebRequest request) {
return new ResponseEntity<>(
getErrorMap(request, e.getMessage(), HttpStatus.NOT_FOUND), HttpStatus.NOT_FOUND);
}
@ExceptionHandler(ShelfException.class)
public ResponseEntity> handleGeneralShelfExceptions(
ShelfException e, WebRequest request) {
return new ResponseEntity<>(
getErrorMap(request, e.getMessage(), HttpStatus.BAD_REQUEST), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(Exception.class)
public ResponseEntity> handleGeneralExceptions(
Exception e, WebRequest request) {
return new ResponseEntity<>(
getErrorMap(request, e.getMessage(), HttpStatus.BAD_REQUEST), HttpStatus.BAD_REQUEST);
}
private Map getErrorMap(WebRequest request, String message, HttpStatus status) {
Map errorInfo = new HashMap<>();
errorInfo.put("Status", status.toString());
errorInfo.put("Error", message);
errorInfo.put("Request", request.getDescription(false));
errorInfo.put("Time", new Date().toString());
return errorInfo;
}
}