Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(store): Return 404 if the triplestore returns 404. (#1828)
  • Loading branch information
Benjamin Geer committed Mar 5, 2021
1 parent efbecee commit 5250f6d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
Expand Up @@ -1028,18 +1028,24 @@ class HttpTriplestoreConnector extends Actor with ActorLogging with Instrumentat
val response = client.execute(targetHost, request, context)
maybeResponse = Some(response)
val statusCode: Int = response.getStatusLine.getStatusCode
val statusCategory: Int = statusCode / 100

if (statusCategory != 2) {
Option(response.getEntity)
.map(responseEntity => EntityUtils.toString(responseEntity, StandardCharsets.UTF_8)) match {
case Some(responseEntityStr) =>
log.error(s"Triplestore responded with HTTP code $statusCode: $responseEntityStr")
throw TriplestoreResponseException(s"Triplestore responded with HTTP code $statusCode: $responseEntityStr")

case None =>
log.error(s"Triplestore responded with HTTP code $statusCode")
throw TriplestoreResponseException(s"Triplestore responded with HTTP code $statusCode")
if (statusCode == 404) {
throw NotFoundException("The requested data was not found")
} else {
val statusCategory: Int = statusCode / 100

if (statusCategory != 2) {
Option(response.getEntity)
.map(responseEntity => EntityUtils.toString(responseEntity, StandardCharsets.UTF_8)) match {
case Some(responseEntityStr) =>
log.error(s"Triplestore responded with HTTP code $statusCode: $responseEntityStr")
throw TriplestoreResponseException(
s"Triplestore responded with HTTP code $statusCode: $responseEntityStr")

case None =>
log.error(s"Triplestore responded with HTTP code $statusCode")
throw TriplestoreResponseException(s"Triplestore responded with HTTP code $statusCode")
}
}
}

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

case notFound: NotFoundException => throw notFound

case e: Exception =>
val message = "Failed to connect to triplestore"
log.error(e, message)
Expand Down
Expand Up @@ -4,7 +4,7 @@ import java.net.URLEncoder
import java.nio.file.Paths

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

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

}
}

0 comments on commit 5250f6d

Please sign in to comment.