Multiplatform Ktor schema declaration • opensavvy.spine.api • AnyEndpoint • Builder • failure
failure¶
abstract fun <S : FailureSpec> failure(spec: S): AnyEndpoint.Builder
Declares a possible failure that can happen when calling this endpoint.
When the failure is thrown, the request ends with the HTTP status code described in the failure: FailureSpec.ByCode.statusCode.
Under the hood, this method uses Ktor's content negotiation features. Therefore, all types that would be valid with content negotiation can be used with this library. Note that you may need to perform some configuration on the Ktor side before using some types, see the official documentation for instructions.
Multiple different failures may be declared, though some failures may or may not support that. See FailureSpec to learn more about combining failures.
Example¶
@Serializable
data class UserAlreadyExists(
val id: String,
) {
companion object : FailureCompanion<UserAlreadyExists>(HttpStatusCode.Conflict)
}
val create by post()
.request<UserCreationDto>()
.response<UserDto>()
.failure(UserAlreadyExists)
See also¶
-
FailureSpecLearn more about failures. -
FailureCompanionLearn more about binding an error to a type.
abstract fun <F> failure(statusCode: HttpStatusCode): AnyEndpoint.Builder
Declares a possible failure that can happen when calling this endpoint.
When the failure is thrown, the request ends with the provided HTTP statusCode.
Under the hood, this method uses Ktor's content negotiation features. Therefore, all types that would be valid with content negotiation can be used with this library. Note that you may need to perform some configuration on the Ktor side before using some types, see the official documentation for instructions.
Multiple different failures may be declared by calling this function multiple times, but no two calls should have the same <strong>statusCode</strong>. See FailureSpec to learn more about combining failures.
Example¶
@Serializable
data class UserAlreadyExists(val id: String)
val create by post()
.request<UserCreationDto>()
.response<UserDto>()
.failure<UserAlreadyExists>(HttpStatusCode.Conflict)
See also¶
FailureSpecLearn more about failures.