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

com.github.fosin.cdp.mvc.exception.CdpExceptionHandler Maven / Gradle / Ivy

The newest version!
package com.github.fosin.cdp.mvc.exception;

import com.github.fosin.cdp.core.exception.CdpControllerException;
import com.github.fosin.cdp.core.exception.CdpServiceException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Description
 *
 * @author fosin
 */
@RestControllerAdvice
public class CdpExceptionHandler {
    @Value("${spring.profiles.active}")
    private String profile;

    @ExceptionHandler({Exception.class})
    public ResponseEntity exception(Exception e) {
        if (isDev()) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
    }

    protected boolean isDev() {
        return profile != null && (profile.contains("dev") || profile.contains("local") || profile.contains("test"));
    }

    @ExceptionHandler({CdpControllerException.class})
    public ResponseEntity cdpControllerException(CdpControllerException e) {
        if (isDev()) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
    }

    @ExceptionHandler({IllegalArgumentException.class})
    public ResponseEntity illegalArgumentException(IllegalArgumentException e) {
        if (isDev()) {
            e.printStackTrace();
        }
        return ResponseEntity.badRequest().body(e.getMessage());
    }

    @ExceptionHandler({CdpServiceException.class})
    public ResponseEntity cdpServiceException(CdpServiceException e) {
        if (isDev()) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy