Skip to content

Commit 5250f6d

Browse files
author
Benjamin Geer
authored
feat(store): Return 404 if the triplestore returns 404. (#1828)
1 parent efbecee commit 5250f6d

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

‎webapi/src/main/scala/org/knora/webapi/store/triplestore/http/HttpTriplestoreConnector.scala‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,18 +1028,24 @@ class HttpTriplestoreConnector extends Actor with ActorLogging with Instrumentat
10281028
val response = client.execute(targetHost, request, context)
10291029
maybeResponse = Some(response)
10301030
val statusCode: Int = response.getStatusLine.getStatusCode
1031-
val statusCategory: Int = statusCode / 100
10321031

1033-
if (statusCategory != 2) {
1034-
Option(response.getEntity)
1035-
.map(responseEntity => EntityUtils.toString(responseEntity, StandardCharsets.UTF_8)) match {
1036-
case Some(responseEntityStr) =>
1037-
log.error(s"Triplestore responded with HTTP code $statusCode: $responseEntityStr")
1038-
throw TriplestoreResponseException(s"Triplestore responded with HTTP code $statusCode: $responseEntityStr")
1039-
1040-
case None =>
1041-
log.error(s"Triplestore responded with HTTP code $statusCode")
1042-
throw TriplestoreResponseException(s"Triplestore responded with HTTP code $statusCode")
1032+
if (statusCode == 404) {
1033+
throw NotFoundException("The requested data was not found")
1034+
} else {
1035+
val statusCategory: Int = statusCode / 100
1036+
1037+
if (statusCategory != 2) {
1038+
Option(response.getEntity)
1039+
.map(responseEntity => EntityUtils.toString(responseEntity, StandardCharsets.UTF_8)) match {
1040+
case Some(responseEntityStr) =>
1041+
log.error(s"Triplestore responded with HTTP code $statusCode: $responseEntityStr")
1042+
throw TriplestoreResponseException(
1043+
s"Triplestore responded with HTTP code $statusCode: $responseEntityStr")
1044+
1045+
case None =>
1046+
log.error(s"Triplestore responded with HTTP code $statusCode")
1047+
throw TriplestoreResponseException(s"Triplestore responded with HTTP code $statusCode")
1048+
}
10431049
}
10441050
}
10451051

@@ -1061,6 +1067,8 @@ class HttpTriplestoreConnector extends Actor with ActorLogging with Instrumentat
10611067
log.error(socketTimeoutException, message)
10621068
throw TriplestoreTimeoutException(message = message, e = socketTimeoutException, log = log)
10631069

1070+
case notFound: NotFoundException => throw notFound
1071+
10641072
case e: Exception =>
10651073
val message = "Failed to connect to triplestore"
10661074
log.error(e, message)

‎webapi/src/test/scala/org/knora/webapi/e2e/v2/MetadataRouteV2E2ESpec.scala‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import java.net.URLEncoder
44
import java.nio.file.Paths
55

66
import akka.http.scaladsl.model.headers.{BasicHttpCredentials, RawHeader}
7-
import akka.http.scaladsl.model.{HttpEntity, HttpResponse}
7+
import akka.http.scaladsl.model.{HttpEntity, HttpResponse, StatusCodes}
88
import org.knora.webapi._
99
import org.knora.webapi.messages.util.rdf._
1010
import org.knora.webapi.routing.RouteUtilV2
@@ -99,5 +99,15 @@ class MetadataRouteV2E2ESpec extends E2ESpec {
9999
assert(response.status.isFailure())
100100
}
101101

102+
"return HTTP 404 if nonexistent metadata is requested" in {
103+
val turtleType = RdfMediaTypes.`text/turtle`.toString()
104+
val header = RawHeader("Accept", turtleType)
105+
val request = Get(
106+
s"$baseApiUrl/v2/metadata/${URLEncoder.encode(SharedTestDataADM.INCUNABULA_PROJECT_IRI, "UTF-8")}") ~> addHeader(
107+
header)
108+
val response: HttpResponse = singleAwaitingRequest(request)
109+
assert(response.status == StatusCodes.NotFound)
110+
}
111+
102112
}
103113
}

0 commit comments

Comments
 (0)