package com.hdl.sdk.connect.cloud; import android.text.TextUtils; import androidx.annotation.IntDef; import androidx.annotation.Nullable; import com.hdl.hdlhttp.callback.HxException; 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 int code; private String msg; @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; //解析错误 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(); } 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(R.string.network_error); this.msg = "网络发生异常,请稍后再试"; } } 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; } @Override public String toString() { return "HDLException{" + "code=" + code + ", msg='" + msg + '\'' + '}'; } @Nullable @Override public String getMessage() { return msg; } }