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

com.txxs.common.response.exception.UniversalException Maven / Gradle / Ivy

The newest version!
package com.txxs.common.response.exception;

import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;

/**
 * @Author:jianghuimin
 * @Date: 2017/8/3
 * @Time:16:57
 */
@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR, reason = "Internal Server Error")
@UniversalException.Code("000")
public abstract class UniversalException extends RuntimeException {
    public static final String DEFAULT_CODE = "000";
    private Map detail;

    protected UniversalException() {
    }

    protected UniversalException(String message) {
        super(message);
    }

    protected UniversalException(String message, Throwable cause) {
        super(message, cause);
    }

    protected UniversalException(Throwable cause) {
        super(cause);
    }

    protected UniversalException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

    public String getCode() {
        UniversalException.Code code = (UniversalException.Code) AnnotatedElementUtils
                .findMergedAnnotation(this.getClass(), UniversalException.Code.class);
        return code.value();
    }

    public String getMessage() {
        String message = super.getMessage();
        if(message == null) {
            message = ((ResponseStatus)AnnotatedElementUtils.findMergedAnnotation(this.getClass(), ResponseStatus.class)).reason();
        }
        return message;
    }

    public Map getDetail() {
        return this.detail;
    }

    public void setDetail(Map detail) {
        this.detail = detail;
    }

    String parseToCode(HttpStatus httpStatus) {
        return  String.valueOf(httpStatus.value());
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE})
    public @interface Code {
        String value();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy