refactor(net): 完善全局异常处理

This commit is contained in:
Quyunshuo
2021-09-26 21:48:05 +08:00
parent cef6b97881
commit f93d334d28
3 changed files with 23 additions and 18 deletions

View File

@ -1,7 +1,6 @@
package com.quyunshuo.androidbaseframemvvm.common.helper
import com.quyunshuo.androidbaseframemvvm.base.utils.toast
import com.quyunshuo.androidbaseframemvvm.common.helper.ResponseExceptionEnum as ExceptionType
/**
* 响应code异常统一处理
@ -26,10 +25,10 @@ suspend fun responseCodeExceptionHandler(
) {
// 进行异常的处理
when (code) {
ExceptionType.INTERNAL_SERVER_ERROR.getCode() -> {
toast(ExceptionType.INTERNAL_SERVER_ERROR.getMessage())
ResponseCodeEnum.ERROR.getCode() -> {
toast(ResponseCodeEnum.ERROR.getMessage())
throw ResponseEmptyException()
}
ExceptionType.SUCCESS.getCode() -> successBlock.invoke()
ResponseCodeEnum.SUCCESS.getCode() -> successBlock.invoke()
}
}

View File

@ -1,37 +1,38 @@
package com.quyunshuo.androidbaseframemvvm.common.helper
/**
* 请求响应异常枚举抽象
* 请求响应code枚举抽象
*
* @author Qu Yunshuo
* @since 2021/7/9 2:56 下午
*/
interface ResponseExceptionEnumCode {
interface IResponseCode {
/**
* 获取该异常枚举的code码
* 获取该枚举的code码
* @return Int
*/
fun getCode(): Int
/**
* 获取该异常枚举的描述
* 获取该枚举的描述
* @return String
*/
fun getMessage(): String
}
/**
* 请求响应异常的类型
* 请求响应code的枚举
*
* @author Qu Yunshuo
* @since 2021/7/9 2:55 下午
*/
enum class ResponseExceptionEnum : ResponseExceptionEnumCode {
enum class ResponseCodeEnum : IResponseCode {
INTERNAL_SERVER_ERROR {
override fun getCode() = 500
override fun getMessage() = "服务器内部错误"
// 通用异常
ERROR {
override fun getCode() = 100
override fun getMessage() = "处理失败"
},
// 成功

View File

@ -1,18 +1,23 @@
package com.quyunshuo.androidbaseframemvvm.common.helper
import com.quyunshuo.androidbaseframemvvm.common.helper.ResponseExceptionEnum as ExceptionType
/**
* 自定义响应异常的抽象类型
*
* @author Qu Yunshuo
* @since 2021/8/27 9:50 上午
*/
interface IResponseException
/**
* 请求响应异常主要为各种code码专门定义的异常
*
* @param type ResponseExceptionEnum 异常类型枚举,用于标记该异常的类型
* @param type IResponseCode 异常类型枚举,用于标记该异常的类型
* @param msg String 异常信息
*
* @author Qu Yunshuo
* @since 2021/7/9 2:57 下午
*/
class ResponseException(val type: ExceptionType, val msg: String) : Exception()
class ResponseException(val type: IResponseCode, val msg: String) : Exception(), IResponseException
/**
* 空异常,表示该异常已经被处理过了,不需要再做额外处理了
@ -20,4 +25,4 @@ class ResponseException(val type: ExceptionType, val msg: String) : Exception()
* @author Qu Yunshuo
* @since 2021/7/9 3:11 下午
*/
class ResponseEmptyException : Exception()
class ResponseEmptyException : Exception(), IResponseException