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

io.elsci.signals.mock.exception.RestExceptionHandler Maven / Gradle / Ivy

There is a newer version: 20231025.1145-36
Show newest version
package io.elsci.signals.mock.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND;

@ControllerAdvice
@ResponseBody
class RestExceptionHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(RestExceptionHandler.class);

    @ExceptionHandler({ EntityNotFoundException.class })
    @ResponseStatus(NOT_FOUND)
    String handleEntityNotException(EntityNotFoundException ex) {
        //can't return an object because it's possible that the client's Accept was bytes or image,
        // and thus it won't be able to convert the object to the requested type
        return ex.getMessage();
    }
    @ExceptionHandler({ Throwable.class })
    @ResponseStatus(INTERNAL_SERVER_ERROR)
    String handleOthers(Throwable ex) {
        LOGGER.error("Couldn't handle request", ex);
        return ex.getMessage();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy