All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.specmatic.core.pattern.HasException.kt Maven / Gradle / Ivy

Go to download

Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.

There is a newer version: 2.0.37
Show newest version
package io.specmatic.core.pattern

data class HasException(val t: Throwable, val message: String = "", val breadCrumb: String? = null) : ReturnValue, ReturnFailure {
    override fun  withDefault(default: U, fn: (T) -> U): U {
        return default
    }

    override fun  ifValue(fn: (T) -> U): ReturnValue {
        return cast()

    }

    override fun update(fn: (T) -> T): ReturnValue {
        return this
    }

    override fun  assimilate(valueResult: ReturnValue, fn: (T, U) -> T): ReturnValue {
        return cast()
    }

    override fun  combine(valueResult: ReturnValue, fn: (T, U) -> V): ReturnValue {
        return cast()
    }

    override fun  cast(): ReturnValue {
        return HasException(t, message, breadCrumb)
    }

    override val value: T
        get() = throw t

    override fun  ifHasValue(fn: (HasValue) -> ReturnValue): ReturnValue {
        return cast()
    }

    override fun addDetails(message: String, breadCrumb: String): ReturnValue {
        val newE = toException(message, breadCrumb, toException())

        return HasException(newE)
    }

    private fun toException(): Throwable {
        return toException(message, breadCrumb ?: "", t)
    }

    private fun toException(
        errorMessage: String,
        breadCrumb: String,
        t: Throwable
    ): Throwable {
        val newE = when (t) {
            is ContractException -> ContractException(errorMessage, breadCrumb, t, t.scenario, t.isCycle)
            else -> ContractException(errorMessage, breadCrumb, t)
        }

        return newE
    }

    override fun  realise(hasValue: (T, String?) -> U, orFailure: (HasFailure) -> U, orException: (HasException) -> U): U {
        return orException(this)
    }
}