You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
GraphQLError in Kotlin is cause an Accidental Override #1022
Can you explain more about accidental override rules in Kotlin? I am not
familiar with the language restorations and how we are not playing ball
correctly
On Sat., 21 Apr. 2018, 17:50 Andres Diaz, ***@***.***> wrote:
When i try to create a custom exception that implements GraphQLError cause
an Accidental Override in the getMessage error in Kotlin
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#1022>, or mute the
thread
<https://github.com/notifications/unsubscribe-auth/ABlbSWKJcHLZa_GCoCIR31jQyuqWmpxMks5tq6msgaJpZM4TeoYM>
.
@bbakerman In Kotlin, the class Throwable has a member called "message". Kotlin automaticaly create a setter and getter with this name (getMessage and setMessage). So, this method cause the accidental override with the abstract method in the interface GraphQLError getMessage() when i extends of Throwable and implements GraphQLError.
I am not sure how we can solve this. The graphql spec says that graphql errors MUST have a "message" attribute. Hence GraphqlError having a getMessage method and hence JSON conversion just working.
Its very convinenting to have exceptions implement GraphqlError to they dont have to be translated manually.
I am not sure how to work around this for Kotlin??
I can confirm this doesn't cause an issue now, but it will cause a problem when you declare it as open or abstract and try to extend this class. How do you extend this custom exception now?
"Their suggestion as a work around was to declare a .java file that represents the custom exception"
haha, i came to this same conclusion as well only a few minutes ago. 😉
only... only in this case you won't have a "pure" Kotlin application - whatever the meaning of this. 🤷♂️
hey guys, i've just ran into this page accidently now: https://kotlinlang.org/docs/java-to-kotlin-interop.html#handling-signature-clashes-with-jvmname
i'm not sure, since i did not cope with this since then, but... could we utilize this to get rid of this issue? @get:JvmName("message") maybe?
will get into context/try it out soon and keep you updated (or delete this comment, if it turns out i'm on the wrong track).
make custom exception class extends the java module extends RuntimeException and implement GraphQLError. It said "because a Kotlin class can’t extend RuntimeException and implement GraphQLError at the same time."
Activity
bbakerman commentedon Apr 23, 2018
andres29diaz commentedon Apr 23, 2018
@bbakerman In Kotlin, the class Throwable has a member called "message". Kotlin automaticaly create a setter and getter with this name (getMessage and setMessage). So, this method cause the accidental override with the abstract method in the interface GraphQLError getMessage() when i extends of Throwable and implements GraphQLError.
Here was the same problem notify by another user: https://stackoverflow.com/questions/48698699/accidental-override-the-following-declarations-have-the-same-jvm-signature-wh
bbakerman commentedon May 6, 2018
I am not sure how we can solve this. The graphql spec says that graphql errors MUST have a "message" attribute. Hence GraphqlError having a getMessage method and hence JSON conversion just working.
Its very convinenting to have exceptions implement GraphqlError to they dont have to be translated manually.
I am not sure how to work around this for Kotlin??
bbakerman commentedon May 6, 2018
I have asked colleagues at Atlassian about this problem.
The consensus was that this is a challenge even in native Kotlin
Their suggestion as a work around was to declare a .java file that represents the custom exception
andres29diaz commentedon May 7, 2018
@bbakerman Great. It would not have even occurred to me to use Java-Kotlin interoperability in this case. I will implement it
Arcnor commentedon Jun 10, 2018
For anybody else stumbling upon this problem, the related Kotlin issue is https://youtrack.jetbrains.com/issue/KT-6653 (I also opened a ticket with the GraphQL specific problem that got closed as duplicated of the first https://youtrack.jetbrains.com/issue/KT-24822)
ryandanielspmc commentedon Nov 7, 2020
Based on the links that @Arcnor provided, there is a solution there that worked for me. Below is my implementation of that solution.
darko1002001 commentedon Aug 26, 2021
I can confirm this doesn't cause an issue now, but it will cause a problem when you declare it as open or abstract and try to extend this class. How do you extend this custom exception now?
binkoni commentedon Feb 10, 2022
This solution is not working anymore in Kotlin 1.6
@JvmField cannot be applied to a property that overrides some other propertybonobonohu commentedon Feb 11, 2022
now i get: "JvmField cannot be applied to a property that overrides some other property". 😞
bbakerman commentedon Feb 11, 2022
See #1022 (comment)
bonobonohu commentedon Feb 11, 2022
"Their suggestion as a work around was to declare a .java file that represents the custom exception"
only... only in this case you won't have a "pure" Kotlin application - whatever the meaning of this. 🤷♂️
bbakerman commentedon Feb 11, 2022
bonobonohu commentedon Apr 3, 2022
hey guys, i've just ran into this page accidently now:
https://kotlinlang.org/docs/java-to-kotlin-interop.html#handling-signature-clashes-with-jvmname
i'm not sure, since i did not cope with this since then, but... could we utilize this to get rid of this issue?
@get:JvmName("message")maybe?will get into context/try it out soon and keep you updated (or delete this comment, if it turns out i'm on the wrong track).
ArisPoz commentedon Apr 25, 2022
So the only workaround to this issue is to create a Java class with the custom exception?
Alright alright, lets go with the pure JVM way then 👍
chuck-park commentedon Jun 28, 2022
make custom exception class extends the java module extends RuntimeException and implement GraphQLError.
It said "because a Kotlin class can’t extend RuntimeException and implement GraphQLError at the same time."
Satook commentedon Jul 14, 2022
There is a useable class already in GraphQL-Java.
GraphqlErrorExceptionWas added to address this ticket: #1743
The file is here: https://github.com/graphql-java/graphql-java/blob/18.x/src/main/java/graphql/GraphqlErrorException.java
It was added in v14 and is still there now for Kotlin compat.