Skip to content

Commit d3367fb

Browse files
committed
fix(LivingDocPromptBuilder): handle exceptions when getting context for PsiElement
The `LivingDocPromptBuilder` now properly handles exceptions when attempting to retrieve the context for a given `PsiElement`. Previously, uncaught exceptions were being ignored, which could lead to unexpected behavior or crashes. This commit ensures that any exceptions are caught and logged, allowing for more robust error handling and improved stability.
1 parent a9271fe commit d3367fb

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/main/kotlin/cc/unitmesh/devti/intentions/action/task/LivingDocPromptBuilder.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import cc.unitmesh.devti.context.base.LLMCodeContext
55
import cc.unitmesh.devti.custom.document.LivingDocumentationType
66
import cc.unitmesh.devti.provider.LivingDocumentation
77
import com.intellij.openapi.application.ReadAction
8+
import com.intellij.openapi.diagnostic.logger
89
import com.intellij.openapi.editor.Editor
910
import com.intellij.openapi.project.Project
1011
import com.intellij.psi.PsiElement
@@ -42,6 +43,8 @@ open class LivingDocPromptBuilder(
4243
open val documentation: LivingDocumentation,
4344
val type: LivingDocumentationType,
4445
) {
46+
private val logger = logger<LivingDocPromptBuilder>()
47+
4548
protected val contextProviders = listOf(
4649
VariableContextProvider(false, false, false),
4750
ClassContextProvider(false),
@@ -90,13 +93,27 @@ open class LivingDocPromptBuilder(
9093
return instruction.trimStart()
9194
}
9295

96+
/**
97+
* Builds a prompt for generating documentation for a given [PsiElement].
98+
*
99+
* @param project The current project.
100+
* @param target The PsiElement for which documentation is to be generated.
101+
* @param fallbackText The fallback text to use if no specific context can be provided.
102+
* @return A string containing the prompt for generating documentation.
103+
*/
93104
open fun buildPrompt(project: Project?, target: PsiElement, fallbackText: String): String {
94105
return ReadAction.compute<String, Throwable> {
95106
val instruction = StringBuilder(fallbackText)
96107

97108
var inOutString = ""
98109
val basicInstruction = this.contextProviders.firstNotNullOfOrNull { contextProvider ->
99-
val llmQueryContext = contextProvider.from(target)
110+
val llmQueryContext = try {
111+
contextProvider.from(target)
112+
} catch (e: Exception) {
113+
logger.info("Error while getting context for $target", e)
114+
null
115+
}
116+
100117
when (llmQueryContext) {
101118
is MethodContext -> {
102119
inOutString = llmQueryContext.inputOutputString()

0 commit comments

Comments
 (0)