Skip to content

Commit

Permalink
Added some more error handling (#1652)
Browse files Browse the repository at this point in the history
Started adding error handling around every single RPC call from the
client to the agent. In this PR I've put some basic error handling in
place for `accept()`, `cancel()`, and `undo()` in `FixupController`.
There is also a bit of corner-case error handling added to
`EditCommandPrompt'.

## Test plan

Locally tested by inserting exceptions into the code paths. Will make
tests for this when we have integration tests, post-GA.
  • Loading branch information
steveyegge committed May 26, 2024
1 parent cb8e4e9 commit 28da5b2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
40 changes: 22 additions & 18 deletions src/main/kotlin/com/sourcegraph/cody/edit/EditCommandPrompt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -548,29 +548,33 @@ class EditCommandPrompt(
@RequiresEdt
fun performOKAction() {
val text = instructionsField.text
if (text.isNotBlank()) {
promptHistory.add(text)
val project = editor.project
// TODO: How do we show user feedback when an error like this happens?
if (project == null) {
logger.warn("Project was null when trying to add an edit session")
return
if (text.isBlank()) {
clearActivePrompt()
return
}
val activeSession = controller.getActiveSession()
promptHistory.add(text)
if (editor.project == null) {
val msg = "Null project for new edit session"
controller.getActiveSession()?.showErrorGroup(msg)
logger.warn(msg)
return
}

activeSession?.let { session ->
session.afterSessionFinished {
startEditCodeSession(text, if (session.isInserted) "insert" else "edit")
}
session.undo()
} ?: run { startEditCodeSession(text) }

val activeSession = controller.getActiveSession()
activeSession?.let {
it.afterSessionFinished {
runInEdt {
EditCodeSession(
controller, editor, text, llmDropdown.item, if (it.isInserted) "insert" else "edit")
}
}
it.undo()
} ?: run { runInEdt { EditCodeSession(controller, editor, text, llmDropdown.item) } }
}
clearActivePrompt()
}

private fun startEditCodeSession(text: String, mode: String = "edit") {
runInEdt { EditCodeSession(controller, editor, text, llmDropdown.item, mode) }
}

private fun makeCornerShape(width: Int, height: Int): RoundRectangle2D {
return RoundRectangle2D.Double(
0.0, 0.0, width.toDouble(), height.toDouble(), CORNER_RADIUS, CORNER_RADIUS)
Expand Down
30 changes: 23 additions & 7 deletions src/main/kotlin/com/sourcegraph/cody/edit/sessions/FixupSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -245,33 +245,49 @@ abstract class FixupSession(
documentListener.setAcceptLensGroupShown(true)
}

private fun showErrorGroup(hoverText: String) {
fun showErrorGroup(hoverText: String) {
showLensGroup(LensGroupFactory(this).createErrorGroup(hoverText))
}

/** Subclass sends a fixup command to the agent, and returns the initial task. */
abstract fun makeEditingRequest(agent: CodyAgent): CompletableFuture<EditTask>

fun accept() {
CodyAgentService.withAgent(project) { agent ->
agent.server.acceptEditTask(TaskIdParam(taskId!!))
try {
CodyAgentService.withAgent(project) { agent ->
agent.server.acceptEditTask(TaskIdParam(taskId!!))
}
} catch (x: Exception) {
// Don't show error lens here; it's sort of pointless.
logger.warn("Error sending editTask/accept for taskId: ${x.localizedMessage}")
dispose()
}
}

fun cancel() {
if (taskId == null) {
dispose()
} else {
CodyAgentService.withAgent(project) { agent ->
agent.server.cancelEditTask(TaskIdParam(taskId!!))
try {
CodyAgentService.withAgent(project) { agent ->
agent.server.cancelEditTask(TaskIdParam(taskId!!))
}
} catch (x: Exception) {
// Error lens here is counterproductive as well.
logger.warn("Error sending editTask/accept for taskId: ${x.localizedMessage}")
dispose()
}
}
}

fun undo() {
runInEdt { showWorkingGroup() }
CodyAgentService.withAgent(project) { agent ->
agent.server.undoEditTask(TaskIdParam(taskId!!))
try {
CodyAgentService.withAgent(project) { agent ->
agent.server.undoEditTask(TaskIdParam(taskId!!))
}
} catch (x: Exception) {
showErrorGroup("Error sending editTask/undo for taskId: ${x.localizedMessage}")
}
}

Expand Down

0 comments on commit 28da5b2

Please sign in to comment.