in.specmatic.core.pattern.HasFailure.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.
Deprecation Notice for group ID "in.specmatic"
******************************************************************************************************
Updates for "specmatic-core" will no longer be available under the deprecated group ID "in.specmatic".
Please update your dependencies to use the new group ID "io.specmatic".
******************************************************************************************************
package `in`.specmatic.core.pattern
import `in`.specmatic.core.Result
data class HasFailure(val failure: Result.Failure, val message: String = "") : ReturnValue, ReturnFailure {
override fun withDefault(default: U, fn: (T) -> U): U {
return default
}
override fun ifValue(fn: (T) -> U): ReturnValue {
return HasFailure(failure)
}
override fun update(fn: (T) -> T): ReturnValue {
return this
}
override fun combineWith(valueResult: ReturnValue, fn: (T, U) -> T): ReturnValue {
return cast()
}
override fun cast(): ReturnValue {
return HasFailure(failure, message)
}
override val value: T
get() = throw ContractException(failure.toFailureReport())
override fun ifHasValue(fn: (HasValue) -> ReturnValue): ReturnValue {
return cast()
}
fun toFailure(): Result.Failure {
return Result.Failure(message, failure)
}
override fun addDetails(message: String, breadCrumb: String): ReturnValue {
return HasFailure(Result.Failure(message, this.toFailure(), breadCrumb))
}
override fun realise(hasValue: (T, String?) -> U, orFailure: (HasFailure) -> U, orException: (HasException) -> U): U {
return orFailure(this)
}
}