Skip to content

Commit

Permalink
fix: fail fast for LdContextNotAvailableException errors in batch ope…
Browse files Browse the repository at this point in the history
…rations
  • Loading branch information
bobeal committed Nov 30, 2023
1 parent c5d7e60 commit 1dadb88
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class EntityOperationHandler(
checkContext(httpHeaders, body).bind()
val context = getContextFromLinkHeader(httpHeaders.getOrEmpty(HttpHeaders.LINK)).bind()
val (parsedEntities, unparsableEntities) =
expandAndPrepareBatchOfEntities(body, context, httpHeaders.contentType)
expandAndPrepareBatchOfEntities(body, context, httpHeaders.contentType).bind()
val (existingEntities, newEntities) = entityOperationService.splitEntitiesByExistence(parsedEntities)
val (unauthorizedEntities, authorizedEntities) = newEntities.partition {
authorizationService.userCanCreateEntities(sub).isLeft()
Expand Down Expand Up @@ -97,7 +97,7 @@ class EntityOperationHandler(
val context = getContextFromLinkHeader(httpHeaders.getOrEmpty(HttpHeaders.LINK)).bind()

val (parsedEntities, unparsableEntities) =
expandAndPrepareBatchOfEntities(body, context, httpHeaders.contentType)
expandAndPrepareBatchOfEntities(body, context, httpHeaders.contentType).bind()
val (existingEntities, newEntities) = entityOperationService.splitEntitiesByExistence(parsedEntities)

val (newUnauthorizedEntities, newAuthorizedEntities) = newEntities.partition {
Expand Down Expand Up @@ -163,7 +163,7 @@ class EntityOperationHandler(
val disallowOverwrite = options.map { it == QUERY_PARAM_OPTIONS_NOOVERWRITE_VALUE }.orElse(false)

val (parsedEntities, unparsableEntities) =
expandAndPrepareBatchOfEntities(body, context, httpHeaders.contentType)
expandAndPrepareBatchOfEntities(body, context, httpHeaders.contentType).bind()
val (existingEntities, newEntities) = entityOperationService.splitEntitiesByExistence(parsedEntities)

val (existingEntitiesUnauthorized, existingEntitiesAuthorized) =
Expand Down Expand Up @@ -304,7 +304,7 @@ class EntityOperationHandler(
payload: List<Map<String, Any>>,
context: String?,
contentType: MediaType?
): BatchEntityPreparation =
): Either<APIException, BatchEntityPreparation> =
payload.map {
val jsonLdExpansionResult =
if (contentType == JSON_LD_MEDIA_TYPE)
Expand All @@ -323,6 +323,10 @@ class EntityOperationHandler(
is Either.Left -> acc.copy(errors = acc.errors.plus(entry.value))
is Either.Right -> acc.copy(success = acc.success.plus(entry.value))
}
}.let { batchEntityPreparation ->
// fail fast for LdContextNotAvailableException errors
batchEntityPreparation.errors.find { it.second is LdContextNotAvailableException }?.second?.left()
?: batchEntityPreparation.right()
}

private suspend fun doBatchCreation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ object JsonLdUtils {
}.fold({
JsonLdEntity(it, contexts).right()
}, {
it.toAPIException().left()
if (it is APIException) it.left()
else it.toAPIException().left()
})

suspend fun expandJsonLdEntityF(input: Map<String, Any>): Either<APIException, JsonLdEntity> =
Expand Down

0 comments on commit 1dadb88

Please sign in to comment.