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

Report location of code causing KSP exceptions when generating assisted factories #73

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.google.devtools.ksp.getAllSuperTypes
import com.google.devtools.ksp.getAnnotationsByType
import com.google.devtools.ksp.getDeclaredProperties
import com.google.devtools.ksp.getVisibility
import com.google.devtools.ksp.symbol.FileLocation
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
Expand Down Expand Up @@ -138,7 +139,7 @@ private fun KSValueParameter.toConstructorParameter(
): ConstructorParameter? {
val type = type.resolve()
if (type.isError) return null
val paramTypeName = type.contextualToTypeName(this, typeParameterResolver)
val paramTypeName = reportLocationOnError { type.contextualToTypeName(this, typeParameterResolver) }
val rawType = paramTypeName.requireRawType()

val isWrappedInProvider = rawType == providerClassName
Expand Down Expand Up @@ -611,3 +612,17 @@ internal fun assertNoDuplicateFunctions(
)
}
}

private fun <D : KSValueParameter, T> D.reportLocationOnError(block: () -> T): T {
return try {
block()
} catch (e: Exception) {
val name = name?.asString() ?: "unknown"
val locationDescription = (location as? FileLocation)?.let {
"${it.filePath}:${it.lineNumber}"
} ?: "unknown location"
throw IllegalArgumentException(
"Failed to resolve type for parameter '$name' at $locationDescription", e
)
}
}