Skip to content

GraphQLError in Kotlin is cause an Accidental Override #1022

Closed

Description

@andres29diaz

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"

Activity

  1. bbakerman commented on Apr 23, 2018

    @bbakerman
    Member
  2. andres29diaz commented on Apr 23, 2018

    @andres29diaz
    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

  3. bbakerman commented on May 6, 2018

    @bbakerman
    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??

  4. bbakerman commented on May 6, 2018

    @bbakerman
    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

  5. andres29diaz commented on May 7, 2018

    @andres29diaz
    Author

    @bbakerman Great. It would not have even occurred to me to use Java-Kotlin interoperability in this case. I will implement it

  6. Arcnor commented on Jun 10, 2018

    @Arcnor

    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)

  7. ryandanielspmc commented on Nov 7, 2020

    @ryandanielspmc

    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
  8. darko1002001 commented on Aug 26, 2021

    @darko1002001

    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?

  9. binkoni commented on Feb 10, 2022

    @binkoni

    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
  10. bonobonohu commented on Feb 11, 2022

    @bonobonohu

    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". 😞

  11. bbakerman commented on Feb 11, 2022

    @bbakerman
    Member
  12. bonobonohu commented on Feb 11, 2022

    @bonobonohu

    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. 🤷‍♂️
  13. bbakerman commented on Feb 11, 2022

    @bbakerman
    Member

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

  14. bonobonohu commented on Apr 3, 2022

    @bonobonohu

    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).

  15. ArisPoz commented on Apr 25, 2022

    @ArisPoz

    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 👍

  16. chuck-park commented on Jun 28, 2022

    @chuck-park

    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."

  17. Satook commented on Jul 14, 2022

    @Satook

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions