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

com.github.nagyesta.lowkeyvault.controller.ErrorHandlingAwareController Maven / Gradle / Ivy

package com.github.nagyesta.lowkeyvault.controller;

import com.github.nagyesta.lowkeyvault.model.common.ErrorModel;
import com.github.nagyesta.lowkeyvault.service.exception.AlreadyExistsException;
import com.github.nagyesta.lowkeyvault.service.exception.CryptoException;
import com.github.nagyesta.lowkeyvault.service.exception.NotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

@Slf4j
public class ErrorHandlingAwareController {

    @ExceptionHandler({IllegalStateException.class, AlreadyExistsException.class, CryptoException.class, NotFoundException.class})
    public ResponseEntity handleException(final Exception exception) {
        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
        final Class exceptionClass = exception.getClass();
        if (exceptionClass.isAnnotationPresent(ResponseStatus.class)) {
            status = exceptionClass.getAnnotation(ResponseStatus.class).value();
        }
        log.error("Returning error model due to exception.", exception);
        return ResponseEntity.status(status).body(ErrorModel.fromException(exception));
    }

    @ExceptionHandler({IllegalArgumentException.class})
    public ResponseEntity handleArgumentException(final Exception exception) {
        log.error("Returning error model due to exception.", exception);
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorModel.fromException(exception));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy