Skip to content

Commit 706f620

Browse files
authored
Merge pull request #967 from modelix/fix/404-on-querying-invalid-branch
MODELIX-993 Requested, but non-existing branch is handled with internal server errors
2 parents 794f804 + 86e1d47 commit 706f620

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

model-server/src/main/kotlin/org/modelix/model/server/handlers/ModelReplicationServer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ class ModelReplicationServer(
287287
val branchRef = repositoryId(repository).getBranchReference(branch)
288288
checkPermission(ModelServerPermissionSchema.branch(branchRef).query)
289289
runWithRepository(repository) {
290-
val version = repositoriesManager.getVersion(branchRef)
290+
val version = repositoriesManager.getVersion(branchRef) ?: throw BranchNotFoundException(branchRef)
291291
LOG.trace("Running query on {} @ {}", branchRef, version)
292-
val initialTree = version!!.getTree()
292+
val initialTree = version.getTree()
293293
val otBranch = OTBranch(
294294
PBranch(initialTree, modelClient.idGenerator),
295295
modelClient.idGenerator,

model-server/src/main/kotlin/org/modelix/model/server/handlers/ui/HistoryHandler.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import org.modelix.model.operations.RevertToOp
6666
import org.modelix.model.operations.applyOperation
6767
import org.modelix.model.persistent.CPVersion.Companion.DESERIALIZER
6868
import org.modelix.model.server.ModelServerPermissionSchema
69+
import org.modelix.model.server.handlers.BranchNotFoundException
6970
import org.modelix.model.server.handlers.IRepositoriesManager
7071
import org.modelix.model.server.templates.PageWithMenuBar
7172
import java.time.LocalDateTime
@@ -128,7 +129,7 @@ class HistoryHandler(val client: IModelClient, private val repositoriesManager:
128129
}
129130

130131
private suspend fun revert(repositoryAndBranch: BranchReference, from: String?, to: String?, author: String?) {
131-
val version = repositoriesManager.getVersion(repositoryAndBranch) ?: throw RuntimeException("Branch doesn't exist: $repositoryAndBranch")
132+
val version = repositoriesManager.getVersion(repositoryAndBranch) ?: throw BranchNotFoundException(repositoryAndBranch)
132133
val branch = OTBranch(PBranch(version.getTree(), client.idGenerator), client.idGenerator, client.storeCache)
133134
branch.runWriteT { t ->
134135
t.applyOperation(RevertToOp(KVEntryReference(from!!, DESERIALIZER), KVEntryReference(to!!, DESERIALIZER)))

model-server/src/test/kotlin/org/modelix/model/server/handlers/ModelReplicationServerTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import io.ktor.client.plugins.defaultRequest
2828
import io.ktor.client.request.accept
2929
import io.ktor.client.request.delete
3030
import io.ktor.client.request.get
31+
import io.ktor.client.request.post
3132
import io.ktor.client.statement.bodyAsText
3233
import io.ktor.http.ContentType
3334
import io.ktor.http.HttpHeaders
@@ -191,6 +192,15 @@ class ModelReplicationServerTest {
191192
}
192193
}
193194

195+
@Test
196+
fun `responds with 404 when querying non-existent branch`() = runWithTestModelServer { _, _ ->
197+
val repositoryId = "abc"
198+
val branch = "non-existing-branch"
199+
val response = client.post("/v2/repositories/$repositoryId/branches/$branch/query")
200+
response shouldHaveStatus 404
201+
assertContains(response.bodyAsText(), "Branch '$branch' does not exist in repository '$repositoryId'")
202+
}
203+
194204
@Test
195205
fun `successfully deletes existing branches`() {
196206
val repositoryId = RepositoryId("repo1")

0 commit comments

Comments
 (0)