Repository: digitalfabrik/entitlementcard
Issue: Backend Logger Throws Exceptions

Javalin throws a debugging exception for most requests.
Fix this.

Changed files:
backend/src/main/kotlin/xyz/elitese/ehrenamtskarte/webservice/GraphQLHandler.kt, backend/src/main/kotlin/xyz/elitese/ehrenamtskarte/webservice/WebService.kt

diff --git a/backend/src/main/kotlin/xyz/elitese/ehrenamtskarte/webservice/GraphQLHandler.kt b/backend/src/main/kotlin/xyz/elitese/ehrenamtskarte/webservice/GraphQLHandler.kt
index 66ae55c4..a821abe1 100644
--- a/backend/src/main/kotlin/xyz/elitese/ehrenamtskarte/webservice/GraphQLHandler.kt
+++ b/backend/src/main/kotlin/xyz/elitese/ehrenamtskarte/webservice/GraphQLHandler.kt
@@ -9,21 +9,21 @@ import graphql.ExceptionWhileDataFetching
 import graphql.ExecutionInput
 import graphql.ExecutionResult
 import graphql.GraphQL
+import io.javalin.http.Context
 import org.dataloader.DataLoaderRegistry
 import xyz.elitese.ehrenamtskarte.webservice.schema.AcceptingStoreQueryService
 import java.io.IOException
-import javax.servlet.http.HttpServletRequest
-import javax.servlet.http.HttpServletResponse
 
 class GraphQLHandler {
     companion object {
-        private val config = SchemaGeneratorConfig(supportedPackages = listOf("xyz.elitese.ehrenamtskarte.webservice.schema"))
+        private val config =
+            SchemaGeneratorConfig(supportedPackages = listOf("xyz.elitese.ehrenamtskarte.webservice.schema"))
         private val queries = listOf(
-                TopLevelObject(AcceptingStoreQueryService())
+            TopLevelObject(AcceptingStoreQueryService())
         )
 
         private val mutations = listOf<TopLevelObject>(
-                // TopLevelObject(LoginMutationService())
+            // TopLevelObject(LoginMutationService())
         )
 
         val graphQLSchema = toSchema(config, queries, mutations)
@@ -42,10 +42,9 @@ class GraphQLHandler {
     /**
      * Get payload from the request.
      */
-    private fun getPayload(request: HttpServletRequest): Map<String, Any>? {
+    private fun getPayload(context: Context): Map<String, Any>? {
         return try {
-            val body = if (request.reader.ready()) request.reader.readText() else ""
-            mapper.readValue<Map<String, Any>>(body)
+            mapper.readValue<Map<String, Any>>(context.body())
         } catch (e: IOException) {
             throw IOException("Unable to parse GraphQL payload.")
         }
@@ -55,7 +54,7 @@ class GraphQLHandler {
      * Get the variables passed in the request.
      */
     private fun getVariables(payload: Map<String, *>) =
-            payload.getOrElse("variables") { emptyMap<String, Any>() } as Map<String, Any>
+        payload.getOrElse("variables") { emptyMap<String, Any>() } as Map<String, Any>
 
 
     /**
@@ -86,27 +85,24 @@ class GraphQLHandler {
     /**
      * Execute a query against schema
      */
-    fun handle(request: HttpServletRequest, response: HttpServletResponse) {
-        val payload = getPayload(request)
+    fun handle(context: Context) {
+        val payload = getPayload(context)
         payload?.let {
             // Execute the query against the schema
             val executionResult = graphQL.executeAsync(
-                    ExecutionInput.Builder()
-                            .query(payload["query"].toString())
-                            .variables(getVariables(payload))
-                            .dataLoaderRegistry(dataLoaderRegistry)
+                ExecutionInput.Builder()
+                    .query(payload["query"].toString())
+                    .variables(getVariables(payload))
+                    .dataLoaderRegistry(dataLoaderRegistry)
             ).get()
             val result = getResult(executionResult)
 
             // write response as json
-            response.setHeader("Content-Type", "application/json")
-            response.writer.write(mapper.writeValueAsString(result))
-            response.status = 200
+            context.json(result)
         }
         if (payload == null) {
-            response.writer.write("Something went wrong.")
-            response.status = 400
+            context.status(400)
+            context.result("Something went wrong.")
         }
-
     }
 }
diff --git a/backend/src/main/kotlin/xyz/elitese/ehrenamtskarte/webservice/WebService.kt b/backend/src/main/kotlin/xyz/elitese/ehrenamtskarte/webservice/WebService.kt
index 89297b91..68fdeeaf 100644
--- a/backend/src/main/kotlin/xyz/elitese/ehrenamtskarte/webservice/WebService.kt
+++ b/backend/src/main/kotlin/xyz/elitese/ehrenamtskarte/webservice/WebService.kt
@@ -17,6 +17,6 @@ class WebService {
         println("Goto http://localhost:7000/graphiql for a graphical editor")
 
         val graphQLHandler = GraphQLHandler()
-        app.post("/") { ctx -> graphQLHandler.handle(ctx.req, ctx.res) }
+        app.post("/") { ctx -> graphQLHandler.handle(ctx) }
     }
 }