Skip to content

Commit

Permalink
Optimize exception messages to contain helpful debug information
Browse files Browse the repository at this point in the history
  • Loading branch information
neetopia committed Nov 20, 2020
1 parent 657931a commit f1fb65f
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 28 deletions.
6 changes: 4 additions & 2 deletions api/src/main/kotlin/com/google/devtools/ksp/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fun KSClassDeclaration.getAllSuperTypes(): Sequence<KSType> {
is KSClassDeclaration -> sequenceOf(resolvedDeclaration)
is KSTypeAlias -> sequenceOf(resolvedDeclaration.findActualType())
is KSTypeParameter -> resolvedDeclaration.getTypesUpperBound()
else -> throw IllegalStateException()
else -> throw IllegalStateException("unhandled type parameter bound $ExceptionMessage")
}
}

Expand All @@ -148,7 +148,7 @@ fun KSClassDeclaration.getAllSuperTypes(): Sequence<KSType> {
is KSClassDeclaration -> it.getAllSuperTypes()
is KSTypeAlias -> it.findActualType().getAllSuperTypes()
is KSTypeParameter -> it.getTypesUpperBound().flatMap { it.getAllSuperTypes() }
else -> throw IllegalStateException()
else -> throw IllegalStateException("unhandled super type kind $ExceptionMessage")
}
}
)
Expand Down Expand Up @@ -243,3 +243,5 @@ fun KSDeclaration.isVisibleFrom(other: KSDeclaration): Boolean {
}

}

const val ExceptionMessage = ",please file a bug at https://github.com/google/ksp/issues/new"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.google.devtools.ksp.visitor

import com.google.devtools.ksp.ExceptionMessage
import com.google.devtools.ksp.symbol.*

class KSValidateVisitor(private val predicate: (KSNode, KSNode) -> Boolean) : KSDefaultVisitor<Unit, Boolean>() {
Expand All @@ -16,7 +17,7 @@ class KSValidateVisitor(private val predicate: (KSNode, KSNode) -> Boolean) : KS
}

override fun defaultHandler(node: KSNode, data: Unit): Boolean {
throw IllegalStateException("unhandled validation condition, please file a bug at https://github.com/google/ksp/issues/new")
throw IllegalStateException("unhandled validation condition $ExceptionMessage")
}

override fun visitTypeReference(typeReference: KSTypeReference, data: Unit): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CodeGeneratorImpl(
val file = File(path)
val parentFile = file.parentFile
if (!parentFile.exists() && !parentFile.mkdirs()) {
throw IllegalStateException()
throw IllegalStateException("failed to make parent directories.")
}
file.writeText("")
fileMap[path] = file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@

package com.google.devtools.ksp.processing.impl

import com.google.devtools.ksp.KspExperimental
import com.google.devtools.ksp.closestClassDeclaration
import com.google.devtools.ksp.isOpen
import com.google.devtools.ksp.isVisibleFrom
import com.google.devtools.ksp.*
import com.intellij.openapi.project.Project
import com.intellij.psi.*
import com.intellij.psi.impl.source.PsiClassReferenceType
Expand Down Expand Up @@ -442,7 +439,7 @@ class ResolverImpl(
return getKSTypeCached(resolveJavaType(type.psi), type.element.typeArguments, type.annotations)
}
}
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unable to resolve type for $type $ExceptionMessage")
}
}

Expand All @@ -454,13 +451,14 @@ class ResolverImpl(
is KtClassOrObject -> KSClassDeclarationImpl.getCached(psi)
is PsiClass -> KSClassDeclarationJavaImpl.getCached(psi)
is KtTypeAlias -> KSTypeAliasImpl.getCached(psi)
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unexpected psi type: ${psi.javaClass} $ExceptionMessage")
}
} else {
when (descriptor) {
is ClassDescriptor -> KSClassDeclarationDescriptorImpl.getCached(descriptor)
is TypeParameterDescriptor -> KSTypeParameterDescriptorImpl.getCached(descriptor)
else -> throw IllegalStateException()
null -> throw IllegalStateException("Failed to resolve descriptor for $kotlinType")
else -> throw IllegalStateException("Unexpected descriptor type: ${descriptor.javaClass} $ExceptionMessage")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.google.devtools.ksp.symbol.impl.binary

import com.google.devtools.ksp.ExceptionMessage
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiAnnotationMethod
import org.jetbrains.kotlin.descriptors.ClassConstructorDescriptor
Expand Down Expand Up @@ -122,6 +123,6 @@ fun ValueParameterDescriptor.getDefaultValue(): Any? {
}
is KtParameter -> ResolverImpl.instance.evaluateConstant(psi.defaultValue, this.type)?.value
is PsiAnnotationMethod -> JavaPsiFacade.getInstance(psi.project).constantEvaluationHelper.computeConstantExpression((psi).defaultValue)
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unexpected psi ${psi.javaClass} $ExceptionMessage")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.google.devtools.ksp.symbol.impl.binary

import com.google.devtools.ksp.ExceptionMessage
import org.jetbrains.kotlin.descriptors.*
import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.symbol.impl.KSObjectCache
Expand Down Expand Up @@ -91,7 +92,7 @@ class KSClassDeclarationDescriptorImpl private constructor(val descriptor: Class
is PropertyDescriptor -> KSPropertyDeclarationDescriptorImpl.getCached(it)
is FunctionDescriptor -> KSFunctionDeclarationDescriptorImpl.getCached(it)
is ClassDescriptor -> getCached(it)
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unexpected descriptor type ${it.javaClass} $ExceptionMessage")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.google.devtools.ksp.symbol.impl.binary

import com.google.devtools.ksp.ExceptionMessage
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.impl.AnonymousFunctionDescriptor
import com.google.devtools.ksp.isOpen
Expand All @@ -31,6 +32,7 @@ import com.google.devtools.ksp.symbol.impl.toKSFunctionDeclaration
import com.google.devtools.ksp.symbol.impl.toKSModifiers
import org.jetbrains.kotlin.load.java.isFromJava
import org.jetbrains.kotlin.resolve.OverridingUtil
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe

class KSFunctionDeclarationDescriptorImpl private constructor(val descriptor: FunctionDescriptor) : KSFunctionDeclaration,
KSDeclarationDescriptorImpl(descriptor),
Expand Down Expand Up @@ -64,7 +66,7 @@ class KSFunctionDeclarationDescriptorImpl private constructor(val descriptor: Fu
descriptor.dispatchReceiverParameter == null -> if (descriptor.isFromJava) FunctionKind.STATIC else FunctionKind.TOP_LEVEL
!descriptor.name.isSpecial && !descriptor.name.asString().isEmpty() -> FunctionKind.MEMBER
descriptor is AnonymousFunctionDescriptor -> FunctionKind.ANONYMOUS
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unable to resolve FunctionKind for ${descriptor.fqNameSafe} $ExceptionMessage")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class KSPropertySetterDescriptorImpl private constructor(descriptor: PropertySet
}

override val parameter: KSValueParameter by lazy {
descriptor.valueParameters.singleOrNull()?.let { KSValueParameterDescriptorImpl.getCached(it) } ?: throw IllegalStateException()
descriptor.valueParameters.singleOrNull()?.let { KSValueParameterDescriptorImpl.getCached(it) }
?: throw IllegalStateException("Failed to resolve property type")
}

override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.google.devtools.ksp.symbol.impl.binary

import com.google.devtools.ksp.ExceptionMessage
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
Expand Down Expand Up @@ -47,7 +48,7 @@ class KSTypeParameterDescriptorImpl private constructor(val descriptor: TypePara
is ClassDescriptor -> KSClassDeclarationDescriptorImpl.getCached(parent)
is FunctionDescriptor -> KSFunctionDeclarationDescriptorImpl.getCached(parent)
is PropertyDescriptor -> KSPropertyDeclarationDescriptorImpl.getCached(parent)
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unexpected containing declaration for ${descriptor.fqNameSafe} $ExceptionMessage")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.google.devtools.ksp.symbol.impl.binary

import com.google.devtools.ksp.ExceptionMessage
import org.jetbrains.kotlin.builtins.isSuspendFunctionTypeOrSubtype
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
Expand All @@ -44,10 +45,10 @@ class KSTypeReferenceDescriptorImpl private constructor(val kotlinType: KotlinTy
when (upperBound) {
is FlexibleType -> KSClassifierReferenceDescriptorImpl.getCached(upperBound.upperBound)
is SimpleType -> KSClassifierReferenceDescriptorImpl.getCached(upperBound)
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unexpected upperbound type ${upperBound.javaClass} $ExceptionMessage")
}
}
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unexpected type: ${kotlinType.constructor.declarationDescriptor?.javaClass} $ExceptionMessage")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.google.devtools.ksp.symbol.impl.java

import com.google.devtools.ksp.ExceptionMessage
import com.intellij.psi.*
import com.intellij.psi.impl.source.PsiClassReferenceType
import com.google.devtools.ksp.processing.impl.ResolverImpl
Expand Down Expand Up @@ -62,7 +63,7 @@ class KSTypeReferenceJavaImpl private constructor(val psi: PsiType) : KSTypeRefe
"char" -> ResolverImpl.instance.module.builtIns.charType
"boolean" -> ResolverImpl.instance.module.builtIns.booleanType
"void" -> ResolverImpl.instance.module.builtIns.unitType
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unexpected primitive type ${this.name} $ExceptionMessage")
}
}

Expand All @@ -88,7 +89,7 @@ class KSTypeReferenceJavaImpl private constructor(val psi: PsiType) : KSTypeRefe
}
}
null -> KSClassifierReferenceDescriptorImpl.getCached((ResolverImpl.instance.builtIns.anyType as KSTypeImpl).kotlinType.makeNullable())
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unexpected psi type for ${type.javaClass} $ExceptionMessage")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.google.devtools.ksp.symbol.impl.kotlin

import com.google.devtools.ksp.ExceptionMessage
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import com.google.devtools.ksp.isOpen
import com.google.devtools.ksp.isVisibleFrom
Expand Down Expand Up @@ -65,7 +66,7 @@ class KSFunctionDeclarationImpl private constructor(val ktFunction: KtFunction)
when (ktFunction) {
is KtNamedFunction, is KtPrimaryConstructor, is KtSecondaryConstructor -> FunctionKind.MEMBER
is KtFunctionLiteral -> if (ktFunction.node.findChildByType(KtTokens.FUN_KEYWORD) != null) FunctionKind.ANONYMOUS else FunctionKind.LAMBDA
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unexpected psi type ${ktFunction.javaClass} $ExceptionMessage")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class KSPropertySetterImpl private constructor(ktPropertySetter: KtPropertyAcces

override val parameter: KSValueParameter by lazy {
ktPropertySetter.parameterList?.parameters?.singleOrNull()?.let { KSValueParameterImpl.getCached(it) }
?: throw IllegalStateException()
?: throw IllegalStateException("Failed to resolve property type")
}

override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.google.devtools.ksp.symbol.impl.kotlin

import com.google.devtools.ksp.ExceptionMessage
import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.symbol.impl.KSObjectCache
import com.google.devtools.ksp.symbol.impl.toKSModifiers
Expand Down Expand Up @@ -71,7 +72,7 @@ class KSTypeParameterImpl private constructor(val ktTypeParameter: KtTypeParamet
is KtClassOrObject -> KSClassDeclarationImpl.getCached(owner)
is KtFunction -> KSFunctionDeclarationImpl.getCached(owner)
is KtProperty -> KSPropertyDeclarationImpl.getCached(owner)
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unexpected containing declaration type ${owner.javaClass} $ExceptionMessage")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.google.devtools.ksp.symbol.impl.kotlin

import com.google.devtools.ksp.ExceptionMessage
import com.google.devtools.ksp.processing.impl.ResolverImpl
import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.symbol.impl.KSObjectCache
Expand Down Expand Up @@ -53,7 +54,7 @@ class KSTypeReferenceImpl private constructor(val ktTypeReference: KtTypeReferen
is KtFunctionType -> KSCallableReferenceImpl.getCached(typeElement)
is KtUserType -> KSClassifierReferenceImpl.getCached(typeElement)
is KtDynamicType -> KSDynamicReferenceImpl.getCached(Unit)
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unexpected type element ${typeElement?.javaClass} $ExceptionMessage")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class KSPropertySetterSyntheticImpl(val ksPropertyDeclaration: KSPropertyDeclara
}

override val parameter: KSValueParameter by lazy {
descriptor.valueParameters.singleOrNull()?.let { KSValueParameterDescriptorImpl.getCached(it) } ?: throw IllegalStateException()
descriptor.valueParameters.singleOrNull()?.let { KSValueParameterDescriptorImpl.getCached(it) }
?: throw IllegalStateException("Failed to resolve property type")
}

override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.google.devtools.ksp.symbol.impl

import com.google.devtools.ksp.ExceptionMessage
import com.intellij.lang.jvm.JvmModifier
import com.intellij.psi.*
import org.jetbrains.kotlin.descriptors.*
Expand Down Expand Up @@ -209,7 +210,7 @@ fun KtClassOrObject.getClassType(): ClassKind {
this.isAnnotation() -> ClassKind.ANNOTATION_CLASS
else -> ClassKind.CLASS
}
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unexpected psi type ${this.javaClass} $ExceptionMessage")
}
}

Expand All @@ -228,7 +229,7 @@ fun org.jetbrains.kotlin.types.Variance.toKSVariance(): Variance {
org.jetbrains.kotlin.types.Variance.IN_VARIANCE -> Variance.CONTRAVARIANT
org.jetbrains.kotlin.types.Variance.OUT_VARIANCE -> Variance.COVARIANT
org.jetbrains.kotlin.types.Variance.INVARIANT -> Variance.INVARIANT
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unexpected variance value $this $ExceptionMessage")
}
}

Expand All @@ -246,7 +247,7 @@ internal fun KotlinType.replaceTypeArguments(newArguments: List<KSTypeArgument>)
val type = when (ksTypeArgument) {
is KSTypeArgumentKtImpl, is KSTypeArgumentJavaImpl, is KSTypeArgumentLiteImpl -> ksTypeArgument.type!!
is KSTypeArgumentDescriptorImpl -> return@mapIndexed ksTypeArgument.descriptor
else -> throw IllegalStateException()
else -> throw IllegalStateException("Unexpected psi for type argument: ${ksTypeArgument.javaClass} $ExceptionMessage")
}.toKotlinType()

TypeProjectionImpl(variance, type)
Expand Down

0 comments on commit f1fb65f

Please sign in to comment.