Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(store): Return 404 if the triplestore returns 404 (DSP-1391) #1828

Merged
merged 1 commit into from Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}

}
}