11package org.graphqlncs.resolver
22
3+ import graphql.ErrorType
4+ import graphql.GraphQLError
35import graphql.kickstart.tools.GraphQLQueryResolver
4- import org.graphqlncs.Exep
5- import org.graphqlncs.ExpTy
6+ import graphql.language.SourceLocation
67import org.graphqlncs.type.*
7- import org.springframework.http.ResponseEntity
88import org.springframework.stereotype.Component
99
10+
1011@Component
1112open class QueryResolver (
1213 private val triangleClassification : TriangleClassification ,
@@ -27,6 +28,10 @@ open class QueryResolver(
2728 }
2829
2930 fun fisher (m : Int , n : Int , x : Double ): Double {
31+
32+ if (m > 1000 || n > 1000 ){
33+ throw CustomException (400 , " Invalid input" )
34+ }
3035 return fisher.exe(m, n, x)
3136 }
3237
@@ -36,17 +41,51 @@ open class QueryResolver(
3641
3742 fun remainder (a : Int , b : Int ): Int {
3843 val lim = 10000
39- return if (a > lim || a < - lim || b > lim || b < - lim) {
40- throw ExpTy (" 400" )
41- } else remainder.exe(a, b)
44+ if (a > lim || a < - lim || b > lim || b < - lim)
45+ throw CustomException (400 , " Invalid input" )
46+
47+ return remainder.exe(a, b)
4248 }
4349
4450 fun bessj (n : Int , x : Double ): Double {
4551
46- return if (n <= 2 || n > 1000 ) {
47- throw ExpTy (" 400" )
52+ if (n <= 2 || n > 1000 ) {
53+ throw CustomException (400 , " Invalid input" )
54+ }
4855
49- } else
50- bessj.bessj(n, x)
56+ return bessj.bessj(n, x)
5157 }
58+ }
59+
60+ /* *
61+ * This does not fully work... but at least we get:
62+ *
63+ * "Unexpected error occurred"
64+ *
65+ * instead of:
66+ *
67+ * "Internal Server Error(s)..."
68+ */
69+ class CustomException (private val errorCode : Int , errorMessage : String ) : RuntimeException(errorMessage), GraphQLError {
70+
71+ override fun getExtensions (): Map <String , Any ?>? {
72+ val customAttributes: MutableMap <String , Any ?> = LinkedHashMap ()
73+ customAttributes[" errorCode" ] = errorCode
74+ customAttributes[" errorMessage" ] = message
75+ return customAttributes
76+ }
77+
78+ override fun getLocations (): List <SourceLocation >? {
79+ return null
80+ }
81+
82+ override fun getErrorType (): ErrorType ? {
83+ return ErrorType .ValidationError
84+ }
85+
86+ @Suppress(" ACCIDENTAL_OVERRIDE" )
87+ override fun getMessage (): String? {
88+ return message
89+ }
90+
5291}
0 commit comments