Skip to content

Commit

Permalink
Merge pull request #132 from Opetushallitus/OK-711-protect-against-ma…
Browse files Browse the repository at this point in the history
…pping-errors

OK-711: drop toteutukset causing MappingExceptions
  • Loading branch information
pkalliok authored Feb 11, 2025
2 parents 27951f9 + e31a2d8 commit ec497f1
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,21 @@ trait ElasticClient extends Logging with KoutaJsonFormats {
search_after = after.map(List(_))
)

def intoToteutusIndexedIfPossible(source: JValue): Option[ToteutusIndexed] =
try {
Some(source.extract[ToteutusIndexed])
} catch {
case e: MappingException => None
}

def listPublished(after: Option[String]): Stream[ToteutusIndexed] = {
logger.info(s"listPublished: querying page after $after")
val result = postJson("toteutus-kouta/_search", toteutusSearch(after))
val hits: List[JValue] = (result \ "hits" \ "hits").children
hits match {
case Nil => Stream.empty
// Stream.concat is eager in its second argument, #::: is truly lazy
case _ => hits.map(_ \ "_source").map(_.extract[ToteutusIndexed]).toStream #:::
case _ => hits.map(_ \ "_source").flatMap(intoToteutusIndexedIfPossible).toStream #:::
listPublished(Some((hits.last \ "sort")(0).extract[String]))
}
}
Expand Down

0 comments on commit ec497f1

Please sign in to comment.