Skip to content

Commit

Permalink
move api execution and responding error in server request to a separa…
Browse files Browse the repository at this point in the history
…te function
  • Loading branch information
diffitask committed Oct 30, 2023
1 parent 90f2cd4 commit 68c2929
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.util.pipeline.*
import org.jetbrains.research.ideFormerPlugin.api.models.IdeApiMethod
import org.jetbrains.research.ideFormerPlugin.server.requests.*
import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper
import org.slf4j.Logger
Expand Down Expand Up @@ -35,6 +37,20 @@ suspend fun ApplicationCall.respondJson(responseObject: Any, status: HttpStatusC
)
}

suspend fun PipelineContext<*, ApplicationCall>.executeAndRespondError(
listDirectoryContents: IdeApiMethod,
logger: Logger
): Boolean = try {
listDirectoryContents.execute()
true
} catch (e: Exception) {
logger.error("Error while api execution: ${e.message}")
call.respondJson(
e.message ?: IdeServerConstants.API_EXECUTION_UNKNOWN_ERROR
)
false
}

object IdeServerConstants {
const val NO_API_AVAILABLE = "No IDE API available"
const val ROOT_PAGE_TEXT = "IDE server"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package org.jetbrains.research.ideFormerPlugin.server.requests
import io.ktor.server.application.*
import io.ktor.server.routing.*
import org.jetbrains.research.ideFormerPlugin.api.models.ChangeDirectory
import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.API_EXECUTION_UNKNOWN_ERROR
import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.PROJECT_DIR_REMAINS_THE_SAME
import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.PROJECT_DIR_WAS_CHANGED_TO
import org.jetbrains.research.ideFormerPlugin.server.executeAndRespondError
import org.jetbrains.research.ideFormerPlugin.server.respondJson
import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper
import org.slf4j.Logger
Expand All @@ -19,11 +19,8 @@ fun Routing.getChangeDirectory(logger: Logger, ideStateKeeper: IdeStateKeeper) {
ChangeDirectory(ideStateKeeper, it)
} ?: ChangeDirectory(ideStateKeeper)

try {
changeDirectory.execute()
} catch (e: Exception) {
logger.error("Error while change directory api execution: ${e.message}")
return@get call.respondJson(e.message ?: API_EXECUTION_UNKNOWN_ERROR)
if (!executeAndRespondError(changeDirectory, logger)) {
return@get
}

ideStateKeeper.saveReversibleApiMethod(changeDirectory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.ktor.server.application.*
import io.ktor.server.routing.*
import org.jetbrains.research.ideFormerPlugin.api.models.KtFileKtMethods
import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants
import org.jetbrains.research.ideFormerPlugin.server.executeAndRespondError
import org.jetbrains.research.ideFormerPlugin.server.respondJson
import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper
import org.slf4j.Logger
Expand All @@ -18,11 +19,8 @@ fun Routing.getKtFileKtMethods(logger: Logger, ideStateKeeper: IdeStateKeeper) {
logger.info("Server GET file kt methods request for file '$fileName' is called")

val ktFileKtMethods = KtFileKtMethods(ideStateKeeper.currentProjectDirectory, fileName)
try {
ktFileKtMethods.execute()
} catch (e: Exception) {
logger.error("Error while kt file kt methods api execution: ${e.message}")
return@get call.respondJson(e.message ?: IdeServerConstants.API_EXECUTION_UNKNOWN_ERROR)
if (!executeAndRespondError(ktFileKtMethods, logger)) {
return@get
}

call.respondJson(ktFileKtMethods.getFileKtMethodsNames()!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.jetbrains.research.ideFormerPlugin.server.requests
import io.ktor.server.application.*
import io.ktor.server.routing.*
import org.jetbrains.research.ideFormerPlugin.api.models.ListDirectoryContents
import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants
import org.jetbrains.research.ideFormerPlugin.server.executeAndRespondError
import org.jetbrains.research.ideFormerPlugin.server.respondJson
import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper
import org.slf4j.Logger
Expand All @@ -17,13 +17,8 @@ fun Routing.getListDirectoryContents(logger: Logger, ideStateKeeper: IdeStateKee
ListDirectoryContents(ideStateKeeper.currentProjectDirectory, it)
} ?: ListDirectoryContents(ideStateKeeper.currentProjectDirectory)

try {
listDirectoryContents.execute()
} catch (e: Exception) {
logger.error("Error while kt file kt methods api execution: ${e.message}")
return@get call.respondJson(
e.message ?: IdeServerConstants.API_EXECUTION_UNKNOWN_ERROR
)
if (!executeAndRespondError(listDirectoryContents, logger)) {
return@get
}

call.respondJson(listDirectoryContents.getSearchDirectoryItemsNames()!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.jetbrains.research.ideFormerPlugin.server.requests
import io.ktor.server.application.*
import io.ktor.server.routing.*
import org.jetbrains.research.ideFormerPlugin.api.models.ProjectModules
import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.API_EXECUTION_UNKNOWN_ERROR
import org.jetbrains.research.ideFormerPlugin.server.executeAndRespondError
import org.jetbrains.research.ideFormerPlugin.server.respondJson
import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper
import org.slf4j.Logger
Expand All @@ -13,11 +13,8 @@ fun Routing.getProjectModules(logger: Logger, ideStateKeeper: IdeStateKeeper) {
logger.info("Server GET project modules request is called")

val projectModules = ProjectModules(ideStateKeeper.userProject)
try {
projectModules.execute()
} catch (e: Exception) {
logger.error("Error while project modules api execution: ${e.message}")
return@get call.respondJson(e.message ?: API_EXECUTION_UNKNOWN_ERROR)
if (!executeAndRespondError(projectModules, logger)) {
return@get
}

call.respondJson(projectModules.getProjectModulesNames()!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.routing.*
import org.jetbrains.research.ideFormerPlugin.api.models.SaveModelFinalAns
import org.jetbrains.research.ideFormerPlugin.server.IdeServerConstants.API_EXECUTION_UNKNOWN_ERROR
import org.jetbrains.research.ideFormerPlugin.server.executeAndRespondError
import org.jetbrains.research.ideFormerPlugin.server.respondJson
import org.jetbrains.research.ideFormerPlugin.stateKeeper.IdeStateKeeper
import org.slf4j.Logger
Expand All @@ -15,11 +15,8 @@ fun Routing.postFinalAnswer(logger: Logger, ideStateKeeper: IdeStateKeeper) {
val modelFinalAns = call.receiveText()

val saveModelFinalAns = SaveModelFinalAns(modelFinalAns)
try {
saveModelFinalAns.execute()
} catch (e: Exception) {
logger.error("Error while saving final answer api execution: ${e.message}")
return@post call.respondJson(e.message ?: API_EXECUTION_UNKNOWN_ERROR)
if (!executeAndRespondError(saveModelFinalAns, logger)) {
return@post
}
logger.info("Save model final ans api call was executed")

Expand Down

0 comments on commit 68c2929

Please sign in to comment.