[Modify]修改网络错误提示

This commit is contained in:
wushaocheng
2022-12-23 16:32:41 +08:00
parent 1c0226adab
commit 8d686a3b60
6 changed files with 127 additions and 11 deletions

View File

@@ -0,0 +1,20 @@
package com.yizhuan.xchat_android_library.net.rxnet.exception
/**
* Created by qisan 2022/5/26
* com.qisan.wanandroid.http.exception
*/
class ApiException : RuntimeException {
private var code: Int? = null
constructor(throwable: Throwable, code: Int) : super(throwable) {
this.code = code
}
constructor(message: String) : super(Throwable(message))
constructor(message: String,code: Int) : super(Throwable(message)){
this.code = code
}
}

View File

@@ -0,0 +1,37 @@
package com.yizhuan.xchat_android_library.net.rxnet.exception
/**
* Created by qisan 2022/5/26
* com.qisan.wanandroid.http.exception
*/
object ErrorStatus {
/**
* 响应成功
*/
const val SUCCESS = 0
/**
* Token 过期
*/
const val TOKEN_INVALID = 401
/**
* 未知错误
*/
const val UNKNOWN_ERROR = 1002
/**
* 服务器内部错误
*/
const val SERVER_ERROR = 1003
/**
* 网络连接超时
*/
const val NETWORK_ERROR = 1004
/**
* API解析异常或者第三方数据结构更改等其他异常
*/
const val API_ERROR = 1005
}

View File

@@ -0,0 +1,62 @@
package com.yizhuan.xchat_android_library.net.rxnet.exception
import android.util.Log
import com.google.gson.JsonParseException
import org.json.JSONException
import retrofit2.HttpException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.text.ParseException
/**
* Created by qisan 2022/5/26
* com.qisan.wanandroid.http.exception
*/
class ExceptionHandle {
companion object {
private const val TAG = "ExceptionHandle"
var errorCode = ErrorStatus.UNKNOWN_ERROR
var errorMsg = "請求失敗,請稍後重試"
fun handleException(e: Throwable): String {
e.printStackTrace()
when (e) {
is SocketTimeoutException, is ConnectException, is HttpException -> { //均视为网络错误
Log.e(TAG, "網絡連接異常: " + e.message)
errorMsg = "網絡異常,請檢查您的網絡再試~"
errorCode = ErrorStatus.NETWORK_ERROR
}
is JsonParseException, is JSONException, is ParseException -> { //均视为解析错误
Log.e(TAG, "數據解析異常: " + e.message)
errorMsg = "數據解析異常"
errorCode = ErrorStatus.SERVER_ERROR
}
is ApiException -> {//服务器返回的错误信息
errorMsg = e.message.toString()
errorCode = ErrorStatus.SERVER_ERROR
}
is UnknownHostException -> {
Log.e(TAG, "網絡連接異常: " + e.message)
errorMsg = "網絡異常,請檢查您的網絡再試~"
errorCode = ErrorStatus.NETWORK_ERROR
}
is IllegalArgumentException -> {
errorMsg = "參數錯誤"
errorCode = ErrorStatus.SERVER_ERROR
}
else -> {//未知错误
try {
Log.e(TAG, "錯誤: " + e.message)
} catch (e1: Exception) {
Log.e(TAG, "未知錯誤Debug調試 ")
}
errorMsg = e.message.toString()
errorCode = ErrorStatus.UNKNOWN_ERROR
}
}
return errorMsg
}
}
}