All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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(this.getClass().getName());
    protected Optional kod;
    protected KnowledgeObjectRepository shelf;

    @Autowired
    public ShelfExceptionHandler(KnowledgeObjectRepository shelf, Optional kod) {
        this.shelf = shelf;
        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.BAD_REQUEST),
                HttpStatus.BAD_REQUEST);
    }

    @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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy