Created
August 28, 2019 20:49
-
-
Save corneil/bf38eaf043ccd75e1de5861b2e1bcb1f to your computer and use it in GitHub Desktop.
precondition extension functions with @ResponseStatus on Exception for Spring MVC.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.http.HttpStatus | |
import org.springframework.web.bind.annotation.ResponseStatus | |
import kotlin.contracts.ExperimentalContracts | |
import kotlin.contracts.contract | |
@ResponseStatus(HttpStatus.PRECONDITION_FAILED) | |
class PreconditionException(message: String) : Exception(message) | |
@ExperimentalContracts | |
@Throws(EntityNotFoundException::class) | |
inline fun precondition(check: Boolean, messageExpr: () -> String) { | |
contract { returns() implies check } | |
if (!check) throw PreconditionException(messageExpr.invoke()) | |
} | |
@ExperimentalContracts | |
@Throws(EntityNotFoundException::class) | |
inline fun precondition(check: Boolean, exception: String) { | |
contract { returns() implies check } | |
if (!check) throw PreconditionException(exception) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
contract
ensures the the following:Can be used without adding
!!
tofuncWithNonNullArguments(someThing)