package mtp.polymer.com.autowidget.utils

import cn.muchinfo.rma.netManage.base.APIException
import cn.muchinfo.rma.netManage.base.APIException.Companion.ERROR_CODE_NET_ENABLE
import cn.muchinfo.rma.netManage.base.DataException
import cn.muchinfo.rma.netManage.base.DataException.Companion.PARSE_ERROR
import cn.muchinfo.rma.netManage.base.InteractiveException

/**
 * 获取错误信息
 */
fun Throwable?.optErrorMessage(): CharSequence {
    if ((this == null)) {
        return ""
    }

    val result: CharSequence = when {
        this is APIException -> when {
            code == ERROR_CODE_NET_ENABLE -> "网络出错"
            errorMsg.isNotEmpty() -> errorMsg
            else -> String.format("网络错误了")
        }
        this is DataException -> when (code) {
            PARSE_ERROR -> "数据分析错误"
            else -> message ?: ""
        }
        this is InteractiveException -> errorMessage
        else -> "未知错误"
    }


     return result

}


