Skip to content

Commit

Permalink
Hack the Search API error handler to figure out moved units
Browse files Browse the repository at this point in the history
This requires an unseemly substitution of the API URL prefix with
the /unit one, so it won't work for types of item yet. That will
have to wait for a better solution.
  • Loading branch information
mikesname committed Mar 15, 2024
1 parent e844952 commit d8ab9bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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

0 comments on commit d8ab9bd

Please sign in to comment.