New file |
| | |
| | | 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; |
| | | //解析错误 |
| | | 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()); |
| | | } |
| | | |
| | | } |
| | | |
| | | |