Skip to content
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

Closed
vagostep opened this issue Apr 21, 2018 · 17 comments
Closed

GraphQLError in Kotlin is cause an Accidental Override #1022

vagostep opened this issue Apr 21, 2018 · 17 comments

Comments

@vagostep
Copy link

vagostep commented Apr 21, 2018

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"

@bbakerman
Copy link
Member

bbakerman commented Apr 23, 2018 via email

@vagostep
Copy link
Author

@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
Copy link
Member

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
Copy link
Member

I have asked colleagues at Atlassian about this problem.

The consensus was that this is a challenge even in native Kotlin

interface GraphQLError {
	fun getMessage(): String
}

class FeatureFlagFetchException(s: String?) : RuntimeException(s), GraphQLError

Their suggestion as a work around was to declare a .java file that represents the custom exception

@vagostep
Copy link
Author

vagostep commented 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
Copy link

Arcnor commented 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
Copy link

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

@darko1002001
Copy link

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
Copy link

binkoni commented Feb 10, 2022

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

This solution is not working anymore in Kotlin 1.6

@JvmField cannot be applied to a property that overrides some other property

@bonobonohu
Copy link

class CustomException(@JvmField override val message: String) : RuntimeException(message), GraphQLError {

now i get: "JvmField cannot be applied to a property that overrides some other property". 😞

@bbakerman
Copy link
Member

See #1022 (comment)

@bonobonohu
Copy link

See #1022 (comment)

"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. 🤷‍♂️

@bbakerman
Copy link
Member

only in this case you won't have a "pure" Kotlin application
Think of it as a pure JVM application however ;)

@bonobonohu
Copy link

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
Copy link

ArisPoz commented 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
Copy link

chuck-park commented 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
Copy link

Satook commented Jul 14, 2022

There is a useable class already in GraphQL-Java. GraphqlErrorException

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
It was added in v14 and is still there now for Kotlin compat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants