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

com.sinszm.common.exception.ApiException Maven / Gradle / Ivy

Go to download

基于SpringBoot扩展生态应用公共组件 Copyright © 2020 智慧程序猿 All rights reserved. https://www.sinsz.com

There is a newer version: 1.1.3.RELEASE
Show newest version
package com.sinszm.common.exception;

import com.sinszm.common.Response;
import org.springframework.util.StringUtils;

/**
 * 公共异常处理
 *
 * @author chenjianbo
 */
public class ApiException extends RuntimeException {

    private ApiError apiError;

    public ApiException() {
        this(SystemApiError.SYSTEM_ERROR_01);
    }

    public ApiException(ApiError apiError) {
        super(apiError.message());
        this.apiError = apiError;
    }

    public ApiException(String errCode, String errMsg) {
        super(errMsg == null ? "" : errMsg);
        this.apiError = new ApiError() {
            @Override
            public String getCode() {
                return StringUtils.isEmpty(errCode) ? "ERROR" : errCode.trim()
                        .replace(" ", "")
                        .replace("_", "-");
            }

            @Override
            public String getMessage() {
                return errMsg == null ? "" : errMsg;
            }
        };
    }

    private ApiError getApiError() {
        return apiError;
    }

    public ApiError apiError() {
        return this.getApiError();
    }

    @Override
    public String toString() {
        return Response.fail(
                this.getApiError(),
                super.getCause()
        ).toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy