package com.yidianling.ydlcommon.custom.http

import com.google.gson.JsonParseException
import com.yidianling.ydlcommon.core.base.BaseApplication
import com.yidianling.ydlcommon.R
import retrofit2.HttpException
import java.io.IOException
import java.net.ConnectException
import java.net.SocketTimeoutException

/**
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2018/02/02
 */
internal object ThrowableUtil {

    @JvmStatic
    fun getMessage(t: Throwable?): String {
        return when (t) {
            is IOException -> BaseApplication.getApp().getString(R.string.data_error)
            is HttpException -> BaseApplication.getApp().getString(R.string.httperror)
            is JsonParseException -> BaseApplication.getApp().getString(R.string.httperror)
            is ConnectException -> BaseApplication.getApp().getString(R.string.network_error)
            is SocketTimeoutException -> BaseApplication.getApp().getString(R.string.timeout)
            is ApiThrowable -> t.message ?: ""
            is CustomThrowable -> t.message ?: ""
            else -> BaseApplication.getApp().getString(R.string.loadfailed)
        }
    }
}