package gaf3.core.cloud.web

import gaf3.core.data.ErrorResult
import gaf3.core.exception.BusinessError.*
import org.springframework.util.ClassUtils

fun errorResult(ex: Throwable): ErrorResult {
    val error = when {
        ex.instanceOf("javax.validation.ConstraintViolationException") ||
        ex.instanceOf("javax.validation.ValidationException") -> ERR_BADPARAM to "请求参数错误"
        else -> ERR_UNKNOW to "系统错误"
    }
    return ErrorResult(errcode = error.first, errmsg = error.second, details = ex.message)
}

fun Throwable.instanceOf(className: String): Boolean {
    return runCatching {
        val cls = ClassUtils.forName(className, null)
        cls.isInstance(this)
    }.getOrElse { false }
}

