New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GraphQLError in Kotlin is cause an Accidental Override #1022
Comments
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. 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 |
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 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 |
@bbakerman Great. It would not have even occurred to me to use Java-Kotlin interoperability in this case. I will implement it |
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) |
Based on the links that @Arcnor provided, there is a solution there that worked for me. Below is my implementation of that solution. class CustomException(@JvmField override val message: String) : RuntimeException(message), GraphQLError {
override fun getMessage(): String? = super.message
override fun getLocations(): MutableList<SourceLocation> = mutableListOf()
override fun getErrorType(): ErrorClassification = CustomErrorClassification()
}
class CustomErrorClassification : ErrorClassification |
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? |
This solution is not working anymore in Kotlin 1.6 @JvmField cannot be applied to a property that overrides some other property |
now i get: "JvmField cannot be applied to a property that overrides some other property". 😞 |
See #1022 (comment) |
"Their suggestion as a work around was to declare a .java file that represents the custom exception"
|
|
hey guys, i've just ran into this page accidently now: |
So the only workaround to this issue is to create a Java class with the custom exception? |
make custom exception class extends the java module extends RuntimeException and implement GraphQLError. |
There is a useable class already in GraphQL-Java. Was 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 |
When i try to create a custom exception that implements GraphQLError cause an Accidental Override in the getMessage error in Kotlin
It's cause because the interface GraphQLError have a method called getMessage(), and the class Throwable also have the same method.
So, the custom exceptions need extends of Throwable and implements GraphQLError and this cause a compiler error "Accidental Override"
The text was updated successfully, but these errors were encountered: