Skip to content

Commit 18b1fe8

Browse files
committed
added utility throw function to avoid entering redundant types for cleanliness
1 parent 980138a commit 18b1fe8

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/main/kotlin/com/papsign/ktor/openapigen/route/Throws.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,24 @@ import kotlin.reflect.full.superclasses
1515

1616
data class ThrowsInfo(override val exceptions: List<APIException<*, *>>) : ThrowInfoProvider
1717

18+
/**
19+
* exists for simpler syntax
20+
*/
21+
inline fun <T: OpenAPIRoute<T>, reified EX : Throwable> T.throws(status: HttpStatusCode, exClass: KClass<EX>, crossinline fn: T.() -> Unit = {}): T {
22+
return throws<T, EX>(status, fn)
23+
}
24+
1825
inline fun <T: OpenAPIRoute<T>, reified EX : Throwable> T.throws(status: HttpStatusCode, crossinline fn: T.() -> Unit = {}): T {
1926
return throws<T, EX, Unit>(status, fn = fn)
2027
}
2128

29+
/**
30+
* exists for simpler syntax
31+
*/
32+
inline fun <T: OpenAPIRoute<T>, reified EX : Throwable, reified B> T.throws(status: HttpStatusCode, example: B? = null, exClass: KClass<EX>, crossinline fn: T.() -> Unit = {}): T {
33+
return throws<T, EX, B>(status, example, null, fn)
34+
}
35+
2236
inline fun <T: OpenAPIRoute<T>, reified EX : Throwable, reified B> T.throws(status: HttpStatusCode, example: B? = null, noinline gen: ((EX) -> B)? = null, crossinline fn: T.() -> Unit = {}): T {
2337
return throws(APIException.apiException(status, example, gen), fn = fn)
2438
}

src/test/kotlin/TestServer.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.papsign.ktor.openapigen.model.Described
1919
import com.papsign.ktor.openapigen.model.server.ServerModel
2020
import com.papsign.ktor.openapigen.openAPIGen
2121
import com.papsign.ktor.openapigen.route.*
22+
import com.papsign.ktor.openapigen.route.path.normal.NormalOpenAPIRoute
2223
import com.papsign.ktor.openapigen.route.path.normal.get
2324
import com.papsign.ktor.openapigen.route.path.normal.post
2425
import com.papsign.ktor.openapigen.route.response.respond
@@ -165,9 +166,7 @@ object TestServer {
165166
route("again") {
166167
tag(TestServer.Tags.EXAMPLE) {
167168

168-
throws(APIException.apiException(HttpStatusCode.BadRequest, "example") {ex: CustomException ->
169-
ex.toString()
170-
}) {
169+
throws(HttpStatusCode.BadRequest, "example", CustomException::class) {
171170
get<StringParam, StringResponse>(
172171
info("String Param Endpoint", "This is a String Param Endpoint"),
173172
example = StringResponse("Hi")

0 commit comments

Comments
 (0)