Skip to content

Commit 30c32b9

Browse files
committed
throw & catch new InvalidPayloadException to return Unsupported Media Type for invalid POST payload
1 parent c45bb27 commit 30c32b9

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

servers/graphql-kotlin-ktor-server/src/main/kotlin/com/expediagroup/graphql/server/ktor/GraphQLStatusPages.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ fun StatusPagesConfig.defaultGraphQLStatusPages(): StatusPagesConfig {
3131
exception<Throwable> { call, cause ->
3232
when (cause) {
3333
is UnsupportedOperationException -> call.respond(HttpStatusCode.MethodNotAllowed)
34+
is InvalidPayloadException -> call.respond(HttpStatusCode.UnsupportedMediaType)
3435
else -> call.respond(HttpStatusCode.BadRequest)
3536
}
3637
}

servers/graphql-kotlin-ktor-server/src/main/kotlin/com/expediagroup/graphql/server/ktor/KtorGraphQLRequestParser.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ internal const val REQUEST_PARAM_QUERY = "query"
3131
internal const val REQUEST_PARAM_OPERATION_NAME = "operationName"
3232
internal const val REQUEST_PARAM_VARIABLES = "variables"
3333

34+
class InvalidPayloadException(message: String, throwable: Throwable) : IllegalStateException(message, throwable)
35+
3436
/**
3537
* GraphQL Ktor [ApplicationRequest] parser.
3638
*/
@@ -62,6 +64,6 @@ open class KtorGraphQLRequestParser(
6264
private suspend fun parsePostRequest(request: ApplicationRequest): GraphQLServerRequest? = try {
6365
request.call.receive()
6466
} catch (e: IOException) {
65-
throw IllegalStateException("Invalid HTTP request - unable to parse GraphQL request from POST payload", e)
67+
throw InvalidPayloadException("Invalid HTTP request - unable to parse GraphQL request from POST payload", e)
6668
}
6769
}

0 commit comments

Comments
 (0)