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

io.specmatic.core.pattern.HasFailure.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

import io.specmatic.core.Result

data class HasFailure(val failure: Result.Failure, val message: String = "") : ReturnValue, ReturnFailure {
    constructor(message: String) : this(Result.Failure(message))

    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  assimilate(acc: ReturnValue, fn: (T, U) -> T): ReturnValue {
        return cast()
    }

    override fun  combine(acc: ReturnValue, fn: (T, U) -> V): 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)
    }
}