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

fix: Verification Policy Error Handling #1228

Merged
merged 1 commit into from
Jun 27, 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package org.hyperledger.identus.pollux.credentialschema.controller
import org.hyperledger.identus.api.http.*
import org.hyperledger.identus.api.http.model.{CollectionStats, Order, Pagination}
import org.hyperledger.identus.pollux.core.model
import org.hyperledger.identus.pollux.core.model.error.VerificationPolicyError
import org.hyperledger.identus.pollux.core.model.error.VerificationPolicyError.*
import org.hyperledger.identus.pollux.core.model.CredentialSchemaAndTrustedIssuersConstraint
import org.hyperledger.identus.pollux.core.service.VerificationPolicyService
import org.hyperledger.identus.pollux.credentialschema.http.{
Expand All @@ -18,21 +16,10 @@ import zio.*
import zio.ZIO.*

import java.util.UUID
import scala.language.implicitConversions

class VerificationPolicyControllerImpl(service: VerificationPolicyService) extends VerificationPolicyController {

def verificationPolicyError2FailureResponse(
vpe: VerificationPolicyError
): ErrorResponse = {
vpe match {
case RepositoryError(cause) =>
ErrorResponse.internalServerError(detail = Option(cause.getMessage))
case NotFoundError(id) =>
ErrorResponse.notFound(detail = Option(s"VerificationPolicy is not found by $id"))
case UnexpectedError(cause) =>
ErrorResponse.internalServerError(detail = Option(cause.getMessage))
}
}
override def createVerificationPolicy(
ctx: RequestContext,
in: VerificationPolicyInput
Expand All @@ -54,26 +41,21 @@ class VerificationPolicyControllerImpl(service: VerificationPolicyService) exten
} yield createdVerificationPolicy
.toSchema()
.withBaseUri(ctx.request.uri)

} mapError (e => verificationPolicyError2FailureResponse(e))
}

override def getVerificationPolicyById(
ctx: RequestContext,
id: UUID
): ZIO[WalletAccessContext, ErrorResponse, VerificationPolicyResponse] = {
service.get(id).flatMap {
case Some(vp) => succeed(vp.toSchema().withUri(ctx.request.uri))
case None => fail(NotFoundError(id))
}
}.mapError(e => verificationPolicyError2FailureResponse(e))
): ZIO[WalletAccessContext, ErrorResponse, VerificationPolicyResponse] =
service.get(id).map(_.toSchema().withUri(ctx.request.uri))

override def updateVerificationPolicyById(
ctx: RequestContext,
id: UUID,
nonce: Int,
update: VerificationPolicyInput
): ZIO[WalletAccessContext, ErrorResponse, VerificationPolicyResponse] = {
val updatedZIO = for {
): ZIO[WalletAccessContext, ErrorResponse, VerificationPolicyResponse] =
for {
constraints <- zio.ZIO.succeed(
update.constraints.toVector // TODO: refactor to Seq
.map(c =>
Expand All @@ -90,28 +72,15 @@ class VerificationPolicyControllerImpl(service: VerificationPolicyService) exten
nonce = nonce + 1
)
updated <- service.update(id, nonce, vp)
} yield updated

updatedZIO
.flatMap {
case Some(vp) => succeed(vp.toSchema().withUri(ctx.request.uri))
case None => fail(NotFoundError(id))
}
.mapError(e => verificationPolicyError2FailureResponse(e))
}
} yield updated.toSchema().withUri(ctx.request.uri)

override def deleteVerificationPolicyById(
ctx: RequestContext,
id: UUID
): ZIO[WalletAccessContext, ErrorResponse, Unit] = {
): ZIO[WalletAccessContext, ErrorResponse, Unit] =
service
.delete(id)
.flatMap {
case Some(_) => succeed(())
case None => fail(NotFoundError(id))
}
.mapError(e => verificationPolicyError2FailureResponse(e))
}
.as(())

override def lookupVerificationPolicies(
ctx: RequestContext,
Expand All @@ -122,17 +91,14 @@ class VerificationPolicyControllerImpl(service: VerificationPolicyService) exten
for {
filteredDomainRecords <- service
.lookup(filter.name, Some(pagination.offset), Some(pagination.limit))
.mapError(verificationPolicyError2FailureResponse)
filteredCount <- service
.filteredCount(filter.name)
.mapError(verificationPolicyError2FailureResponse)
baseUri = ctx.request.uri.copy(querySegments = Seq.empty)
filteredRecords = filteredDomainRecords.map(
_.toSchema().withBaseUri(baseUri)
)
totalCount <- service
.totalCount()
.mapError(verificationPolicyError2FailureResponse)
response = VerificationPolicyPageRequestLogic(
ctx,
pagination,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package org.hyperledger.identus.pollux.core.model.error

import org.hyperledger.identus.shared.models.{Failure, StatusCode}

import java.util.UUID

sealed trait VerificationPolicyError
sealed trait VerificationPolicyError(
val statusCode: StatusCode,
val userFacingMessage: String
) extends Failure {
override val namespace = "CredentialSchema"
}

object VerificationPolicyError {
final case class RepositoryError(cause: Throwable) extends VerificationPolicyError
final case class NotFoundError(id: UUID) extends VerificationPolicyError
final case class UnexpectedError(cause: Throwable) extends VerificationPolicyError
final case class NotFoundError(id: UUID)
extends VerificationPolicyError(StatusCode.NotFound, s"VerificationPolicy is not found by $id")
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ import java.util.UUID

trait VerificationPolicyRepository {

def create(verificationPolicy: VerificationPolicy): RIO[WalletAccessContext, VerificationPolicy]
def create(verificationPolicy: VerificationPolicy): URIO[WalletAccessContext, VerificationPolicy]

def get(id: UUID): RIO[WalletAccessContext, Option[VerificationPolicy]]
def findById(id: UUID): URIO[WalletAccessContext, Option[VerificationPolicy]]

def exists(id: UUID): RIO[WalletAccessContext, Boolean]
def exists(id: UUID): URIO[WalletAccessContext, Boolean]

def getHash(id: UUID): RIO[WalletAccessContext, Option[Int]]
def findHashById(id: UUID): URIO[WalletAccessContext, Option[Int]]

def update(
id: UUID,
nonce: Int,
verificationPolicy: VerificationPolicy
): RIO[WalletAccessContext, Option[VerificationPolicy]]
): URIO[WalletAccessContext, VerificationPolicy]

def delete(id: UUID): RIO[WalletAccessContext, Option[VerificationPolicy]]
def delete(id: UUID): URIO[WalletAccessContext, VerificationPolicy]

def totalCount(): RIO[WalletAccessContext, Long]
def totalCount(): URIO[WalletAccessContext, Long]

def filteredCount(nameOpt: Option[String]): RIO[WalletAccessContext, Long]
def filteredCount(nameOpt: Option[String]): URIO[WalletAccessContext, Long]

def lookup(
nameOpt: Option[String],
offsetOpt: Option[Int],
limitOpt: Option[Int]
): RIO[WalletAccessContext, List[VerificationPolicy]]
): URIO[WalletAccessContext, List[VerificationPolicy]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ trait VerificationPolicyService {
constraints: Seq[VerificationPolicyConstraint] = Seq.empty
): ZIO[WalletAccessContext, VerificationPolicyError, VerificationPolicy]

def get(id: UUID): ZIO[WalletAccessContext, VerificationPolicyError, Option[VerificationPolicy]]
def find(id: UUID): ZIO[WalletAccessContext, VerificationPolicyError, Option[VerificationPolicy]]

def get(id: UUID): ZIO[WalletAccessContext, VerificationPolicyError, VerificationPolicy]

def update(
id: UUID,
nonce: Int,
verificationPolicy: VerificationPolicy
): ZIO[WalletAccessContext, VerificationPolicyError, Option[VerificationPolicy]]
): ZIO[WalletAccessContext, VerificationPolicyError, VerificationPolicy]

def delete(id: UUID): ZIO[WalletAccessContext, VerificationPolicyError, Option[VerificationPolicy]]
def delete(id: UUID): ZIO[WalletAccessContext, VerificationPolicyError, VerificationPolicy]

def totalCount(): ZIO[WalletAccessContext, VerificationPolicyError, Long]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.hyperledger.identus.pollux.core.service

import org.hyperledger.identus.pollux.core.model.{VerificationPolicy, VerificationPolicyConstraint}
import org.hyperledger.identus.pollux.core.model.error.VerificationPolicyError
import org.hyperledger.identus.pollux.core.model.error.VerificationPolicyError.NotFoundError
import org.hyperledger.identus.pollux.core.repository.VerificationPolicyRepository
import org.hyperledger.identus.shared.models.WalletAccessContext
import zio.*
Expand All @@ -17,9 +18,6 @@ class VerificationPolicyServiceImpl(
repository: VerificationPolicyRepository
) extends VerificationPolicyService {

private val throwableToVerificationPolicyError: Throwable => VerificationPolicyError =
VerificationPolicyError.RepositoryError.apply

override def create(
name: String,
description: String,
Expand All @@ -29,34 +27,44 @@ class VerificationPolicyServiceImpl(
verificationPolicy <- VerificationPolicy.make(name, description, constraints)
createdVerificationPolicy <- repository.create(verificationPolicy)
} yield createdVerificationPolicy
}.mapError(throwableToVerificationPolicyError)
}

override def find(id: UUID): ZIO[WalletAccessContext, VerificationPolicyError, Option[VerificationPolicy]] =
repository
.findById(id)

override def get(id: UUID): ZIO[WalletAccessContext, VerificationPolicyError, Option[VerificationPolicy]] =
override def get(id: UUID): ZIO[WalletAccessContext, VerificationPolicyError, VerificationPolicy] = {
repository
.get(id)
.mapError(throwableToVerificationPolicyError)
.findById(id)
.flatMap {
case None => ZIO.fail(NotFoundError(id))
case Some(value) => ZIO.succeed(value)
}
}

override def update(
id: UUID,
nonce: Int,
verificationPolicy: VerificationPolicy
): ZIO[WalletAccessContext, VerificationPolicyError, Option[VerificationPolicy]] =
repository
.update(id, nonce, verificationPolicy)
.mapError(throwableToVerificationPolicyError)
): ZIO[WalletAccessContext, VerificationPolicyError, VerificationPolicy] =
for {
_ <- get(id)
result <- repository.update(id, nonce, verificationPolicy)
} yield result

override def delete(id: UUID): ZIO[WalletAccessContext, VerificationPolicyError, Option[VerificationPolicy]] =
repository
.delete(id)
.mapError(throwableToVerificationPolicyError)
override def delete(id: UUID): ZIO[WalletAccessContext, VerificationPolicyError, VerificationPolicy] =
for {
_ <- get(id)
result <- repository.delete(id)
} yield result

override def totalCount(): ZIO[WalletAccessContext, VerificationPolicyError, Long] =
repository.totalCount().mapError(throwableToVerificationPolicyError)
repository.totalCount()

override def filteredCount(
name: Option[String]
): ZIO[WalletAccessContext, VerificationPolicyError, Long] =
repository.filteredCount(name).mapError(throwableToVerificationPolicyError)
repository.filteredCount(name)

override def lookup(
name: Option[String],
Expand All @@ -65,5 +73,5 @@ class VerificationPolicyServiceImpl(
): ZIO[WalletAccessContext, VerificationPolicyError, List[VerificationPolicy]] =
repository
.lookup(name, offset, limit)
.mapError(throwableToVerificationPolicyError)

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ object VerificationPolicySql extends DoobieContext.Postgres(SnakeCase) {
run(quote(query[VerificationPolicyConstraint].filter(_.fk_id == lift(fk_id)).delete))
}

def getById(id: UUID) =
def findById(id: UUID) =
run(
quote(
query[VerificationPolicy]
.filter(_.id == lift(id))
)
).map(_.headOption)

def getHashById(id: UUID) =
def findHashById(id: UUID) =
run(
quote(
query[VerificationPolicy]
Expand All @@ -72,7 +72,7 @@ object VerificationPolicySql extends DoobieContext.Postgres(SnakeCase) {
)
)

def getVerificationPolicyConstrains(fk_ids: Seq[UUID]) =
def getVerificationPolicyConstraints(fk_ids: Seq[UUID]) =
run(
quote(
query[VerificationPolicyConstraint]
Expand Down
Loading
Loading