io.specmatic.core.pattern.HasException.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of specmatic-core Show documentation
Show all versions of specmatic-core Show documentation
Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.
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)
}
}