From 773b9953ad645b39a9efa8ab6d71dfc9d9e4e22e Mon Sep 17 00:00:00 2001 From: hxb <hxb@hdlchina.com.cn> Date: 星期四, 24 十月 2024 11:51:57 +0800 Subject: [PATCH] 补全了所有日志 --- HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/core/exception/HDLException.java | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 163 insertions(+), 0 deletions(-) diff --git a/HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/core/exception/HDLException.java b/HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/core/exception/HDLException.java new file mode 100644 index 0000000..9a9b3e9 --- /dev/null +++ b/HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/core/exception/HDLException.java @@ -0,0 +1,163 @@ +package com.hdl.linkpm.sdk.core.exception; + +import android.text.TextUtils; + +import androidx.annotation.IntDef; +import androidx.annotation.Nullable; + +import com.hdl.hdlhttp.HxHttpConfig; +import com.hdl.hdlhttp.callback.HxException; +import com.hdl.linkpm.sdk.R; +import com.hdl.linkpm.sdk.core.response.BaseInfo; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Created by Tong on 2021/11/8. + */ +public class HDLException extends RuntimeException { + + private Throwable rawThrowable; + private int code; + private String msg; + + private BaseInfo.Extra mExtra; + + @Retention(RetentionPolicy.SOURCE) + @Target({ElementType.FIELD, ElementType.PARAMETER}) + @IntDef({ + ErrorCode.UNKNOWN, + ErrorCode.PARSE_ERROR, + ErrorCode.NETWORK_ERROR, + ErrorCode.HTTP_ERROR, + ErrorCode.SSL_ERROR, + ErrorCode.TIMEOUT_ERROR + }) + public @interface ErrorCode { + //鏈煡閿欒 + int UNKNOWN = -40000; + //瑙f瀽閿欒 + int PARSE_ERROR = -40001; + //缃戠粶閿欒 + int NETWORK_ERROR = -40002; + //鍗忚鍑洪敊 + int HTTP_ERROR = -40003; + //璇佷功鍑洪敊 + int SSL_ERROR = -40004; + //杩炴帴瓒呮椂 + int TIMEOUT_ERROR = -40005; + + } + + + private int getHxToErrorCode(@HxException.ErrorCode int hxCode) { + switch (hxCode) { + case HxException.ErrorCode.UNKNOWN: + return ErrorCode.UNKNOWN; + case HxException.ErrorCode.PARSE_ERROR: + return ErrorCode.PARSE_ERROR; + case HxException.ErrorCode.NETWORK_ERROR: + return ErrorCode.NETWORK_ERROR; + case HxException.ErrorCode.HTTP_ERROR: + return ErrorCode.HTTP_ERROR; + case HxException.ErrorCode.SSL_ERROR: + return ErrorCode.SSL_ERROR; + case HxException.ErrorCode.TIMEOUT_ERROR: + return ErrorCode.TIMEOUT_ERROR; + } + return ErrorCode.NETWORK_ERROR; + } + + public HDLException(HxException exception) { + this.code = getHxToErrorCode(exception.getCode()); + this.msg = exception.getMsg(); + this.rawThrowable = exception.getRawThrowable(); + } + + + public HDLException(int code, String msg) { + this.code = code; + this.msg = msg; + if (!TextUtils.isEmpty(msg)) { + this.msg = msg; + } else { + this.msg = HxHttpConfig.getInstance().getString(com.hdl.hdlhttp.R.string.network_error); + } + } + + public BaseInfo.Extra getmExtra() { + return mExtra; + } + + public void setExtra(BaseInfo.Extra mExtra) { + this.mExtra = mExtra; + } + + public HDLException(int code, String msg, BaseInfo.Extra extra) { + this.code = code; + this.msg = msg; + if (extra != null) { + this.mExtra = extra; + } + if (!TextUtils.isEmpty(msg)) { + this.msg = msg; + } else { + this.msg = HxHttpConfig.getInstance().getString(com.hdl.hdlhttp.R.string.network_error); + } + } + + + public boolean isNetError() { + return getCode() < 0; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + @Nullable + @Override + public String getMessage() { + if (rawThrowable != null) { + String message = rawThrowable.getMessage(); + if (!TextUtils.isEmpty(message)) { + return message; + } + if (!TextUtils.isEmpty(msg)) { + return msg; + } + } + return super.getMessage(); + } + + @Override + public String toString() { + return "HDLException{" + + "rawThrowable=" + rawThrowable + + ", code=" + code + + ", msg='" + msg + '\'' + + '}'; + } + + public static HDLException getErrorWithCode(HDLErrorCode code) { + return new HDLException(code.getCode(), code.getMsg()); + } + +} + + -- Gitblit v1.8.0