47 lines
1.1 KiB
Kotlin
47 lines
1.1 KiB
Kotlin
package com.aiosman.ravenow.data
|
|
|
|
import com.aiosman.ravenow.data.api.ErrorCode
|
|
import com.aiosman.ravenow.data.api.toErrorCode
|
|
import com.google.gson.Gson
|
|
import com.google.gson.annotations.SerializedName
|
|
import okhttp3.ResponseBody
|
|
|
|
/**
|
|
* 错误返回
|
|
*/
|
|
class ServiceException(
|
|
override val message: String,
|
|
val code: Int? = 0,
|
|
val data: Any? = null,
|
|
val error: String? = null,
|
|
val name: String? = null,
|
|
val errorType: ErrorCode = ErrorCode.UNKNOWN
|
|
) : Exception(
|
|
message
|
|
)
|
|
|
|
data class ApiErrorResponse(
|
|
@SerializedName("code")
|
|
val code: Int?,
|
|
@SerializedName("error")
|
|
val error: String?,
|
|
@SerializedName("message")
|
|
val name: String?,
|
|
) {
|
|
fun toServiceException(): ServiceException {
|
|
return ServiceException(
|
|
message = error ?: name ?: "",
|
|
code = code,
|
|
error = error,
|
|
name = name,
|
|
errorType = (code ?: 0).toErrorCode()
|
|
)
|
|
}
|
|
}
|
|
|
|
fun parseErrorResponse(errorBody: ResponseBody?): ApiErrorResponse? {
|
|
return errorBody?.let {
|
|
val gson = Gson()
|
|
gson.fromJson(it.charStream(), ApiErrorResponse::class.java)
|
|
}
|
|
} |