feat: 添加对网络请求统一code码处理的函数
This commit is contained in:
@ -0,0 +1,29 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.common.helper
|
||||
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.toast
|
||||
import kotlin.jvm.Throws
|
||||
import com.quyunshuo.androidbaseframemvvm.common.helper.ResponseExceptionEnum as ExceptionType
|
||||
|
||||
/**
|
||||
* 请求异常处理
|
||||
*
|
||||
* 该方法主要做两件事:
|
||||
*
|
||||
* - 1.做统一的code码处理
|
||||
* - 2.未进行统一处理的code码会被转换为自定义异常[ResponseException]抛出
|
||||
*
|
||||
* 使用方式为:进行统一处理的异常进行抛出[ResponseEmptyException],未进行处理的抛出[ResponseException]
|
||||
*
|
||||
* @param code Int code码
|
||||
* @throws ResponseException 未进行处理的异常会进行抛出,让ViewModel去做进一步处理
|
||||
*/
|
||||
@Throws(ResponseException::class)
|
||||
fun responseExceptionHandler(code: Int) {
|
||||
// 进行异常的处理
|
||||
when (code) {
|
||||
ExceptionType.INTERNAL_SERVER_ERROR.getCode() -> {
|
||||
toast(ExceptionType.INTERNAL_SERVER_ERROR.getMessage())
|
||||
throw ResponseEmptyException()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.common.helper
|
||||
|
||||
import com.quyunshuo.androidbaseframemvvm.common.helper.ResponseExceptionEnum as ExceptionType
|
||||
|
||||
/**
|
||||
* 请求响应异常,主要为各种code码专门定义的异常
|
||||
*
|
||||
* @property type ResponseExceptionEnum 异常类型枚举,用于标记该异常的类型
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 2021/7/9 2:57 下午
|
||||
*/
|
||||
class ResponseException(val type: ExceptionType) : Exception()
|
||||
|
||||
/**
|
||||
* 空异常,表示该异常已经被处理过了,不需要再做额外处理了
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 2021/7/9 3:11 下午
|
||||
*/
|
||||
class ResponseEmptyException : Exception()
|
||||
@ -0,0 +1,14 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.common.helper
|
||||
|
||||
/**
|
||||
* 请求响应异常的类型
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 2021/7/9 2:55 下午
|
||||
*/
|
||||
enum class ResponseExceptionEnum : ResponseExceptionEnumCode {
|
||||
INTERNAL_SERVER_ERROR {
|
||||
override fun getCode() = 500
|
||||
override fun getMessage() = "服务器内部错误"
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.common.helper
|
||||
|
||||
/**
|
||||
* 请求响应异常枚举的抽象
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 2021/7/9 2:56 下午
|
||||
*/
|
||||
interface ResponseExceptionEnumCode {
|
||||
|
||||
/**
|
||||
* 获取该异常枚举的code码
|
||||
* @return Int
|
||||
*/
|
||||
fun getCode(): Int
|
||||
|
||||
/**
|
||||
* 获取该异常枚举的描述
|
||||
* @return String
|
||||
*/
|
||||
fun getMessage(): String
|
||||
}
|
||||
Reference in New Issue
Block a user