Files
rider-pro-android-app/app/src/main/java/com/aiosman/riderpro/data/Exception.kt

43 lines
974 B
Kotlin
Raw Normal View History

2024-08-11 17:15:17 +08:00
package com.aiosman.riderpro.data
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import okhttp3.ResponseBody
2024-08-24 23:11:20 +08:00
/**
* 错误返回
*/
2024-08-11 17:15:17 +08:00
class ServiceException(
override val message: String,
val code: Int? = 0,
val data: Any? = null,
val error: String? = null,
val name: String? = null,
2024-08-11 17:15:17 +08:00
) : 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
)
}
}
fun parseErrorResponse(errorBody: ResponseBody?): ApiErrorResponse? {
return errorBody?.let {
val gson = Gson()
gson.fromJson(it.charStream(), ApiErrorResponse::class.java)
}
}