我在尝试扩展 RuntimeException 并实现 GraphQLError 时遇到了以下错误接口(interface),用 Java 定义,来 self 的 Kotlin 代码。这是错误:
Accidental override: The following declarations have the same JVM signature (getMessage()Ljava.lang.string;):
public open fun <get-message>(): String? defined in NegativeCountException
public open fun getMessage(): String? defined in NegativeCountException
以下是我的代码:
class NegativeCountException() : RuntimeException(), GraphQLError {
override fun getMessage(): String? {
TODO("not implemented")
}
<...>
}
在哪里 GraphQLError
是一个接口(interface),在Java中定义如下:
public interface GraphQLError {
String getMessage();
<...>
}
似乎与 getMessage()
冲突定义于 Throwable
.
我无法更改接口(interface)的代码,因为它来自库。
如何创建自己的运行时异常,它将实现 GraphQLError ?
PS:我也尝试了以下,并收到了一个非常相似的错误:
class NegativeCountException(override val message: String?) : RuntimeException(), GraphQLError {
<...>
}
最佳答案
这是一个graphql问题。我的解决方法:
重新实现 GraphQLError、ExceptionWhileDataFetching 和 DataFetcherErrorHandler。
KGraphQLError 是用于自定义错误的“固定”kotlin 接口(interface)(使用 val 而不是 getter)。
在 KDataFetcherErrorHandler 中:将此行中的 ExceptionWhileDataFetching
替换为 KExceptionWhileDataFetching
:
val error = ExceptionWhileDataFetching(path, exception, sourceLocation)
KExceptionWhileErrorHandling
实现 GraphQLError
。查看代码并将 if (exception is GraphQLError)
的所有实例替换为 (exception is KGraphQLError)
将新的 KDataFetcherErrorHandler
传递给您的 queryExecutionStrategy 和 mutationExecutionStrategy。
您的自定义错误现在可以扩展 Throwable 并实现 KGraphQLError,并得到正确处理。
更多信息在这里:http://graphql-java.readthedocs.io/en/latest/execution.html
关于java - "Accidental override: The following declarations have the same JVM signature"实现Java接口(interface)时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48698699/