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

Hack the Search API error handler to figure out moved units #1504

Merged
merged 1 commit into from
Mar 15, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion modules/api/app/controllers/api/v1/ApiV1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import services.RateLimitChecker
import services.accounts.AccountManager
import services.cypher.CypherService
import services.data._
import services.redirects.MovedPageLookup
import services.search.SearchConstants._
import services.search._
import utils.{DateFacetUtils, FieldFilter, Page, PageParams}
Expand Down Expand Up @@ -55,6 +56,7 @@ case class ApiV1 @Inject()(
ws: WSClient,
cypher: CypherService,
rateLimits: RateLimitChecker,
pageRelocator: MovedPageLookup,
mat: Materializer,
dfu: DateFacetUtils
) extends CoreActionBuilders
Expand Down Expand Up @@ -495,7 +497,21 @@ case class ApiV1 @Inject()(
immediate(Redirect(controllers.api.routes.ApiHome.index()))

private def errorHandler(implicit request: RequestHeader): PartialFunction[Throwable, Future[Result]] = {
case e: ItemNotFound => immediate(error(NOT_FOUND, e.message))
case e: ItemNotFound =>
// FIXME:
// This is a hack because we store moved items by the hash of their page URL so we can't
// easily determine if an item has moved when the API location isn't stored. Here is
// a heuristic to check if a Documentary Unit has been moved, via the hard-coded /units URL.
// Unlikely this ever changes now but if it does this will break.
// If/when the redirect DB is updated to store item IDs this can be reworked.
val unitPrefix = "/units"
val unitPath = request.path.replace(routes.ApiV1Home.index().url, unitPrefix)
pageRelocator.hasMovedTo(unitPath).map {
case Some(newPath) =>
val newApiPath = newPath.replace(unitPrefix, routes.ApiV1Home.index().url)
error(MOVED_PERMANENTLY, Some(newApiPath)).withHeaders(HeaderNames.LOCATION -> newApiPath)
case None => error(NOT_FOUND, e.message)
}
case e: PermissionDenied => immediate(error(FORBIDDEN))
case _ => immediate(error(INTERNAL_SERVER_ERROR))
}
Expand Down
2 changes: 2 additions & 0 deletions modules/api/conf/messages
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
api.error.301=Moved Permanently
api.error.302=Found
api.error.400=Bad Request
api.error.401=Not Authenticated
api.error.403=Forbidden
Expand Down
Loading