Skip to content

Commit

Permalink
fix: present error handling Part 2 (#1177)
Browse files Browse the repository at this point in the history
Signed-off-by: Bassam Riman <[email protected]>
  • Loading branch information
CryptoKnightIOG authored Jun 14, 2024
1 parent dffad1d commit 9ac6e52
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ object PresentBackgroundJobs extends BackgroundJobsHelper {
presentation <-
for {
presentation <- presentationService
.createSDJwtPresentation(id, requestPresentation, prover)
.createSDJwtPresentation(id, requestPresentation)
.provideSomeLayer(ZLayer.succeed(walletAccessContext))
} yield presentation
_ <- presentationService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.hyperledger.identus.presentproof.controller

import org.hyperledger.identus.api.http.{ErrorResponse, RequestContext}
import org.hyperledger.identus.api.http.model.PaginationInput
import org.hyperledger.identus.pollux.core.model.error.PresentationError
import org.hyperledger.identus.presentproof.controller.http.*
import org.hyperledger.identus.shared.models.WalletAccessContext
import zio.ZIO
Expand Down Expand Up @@ -35,53 +34,6 @@ trait PresentProofController {
}

object PresentProofController {
def toHttpError(error: PresentationError): ErrorResponse =
error match
case PresentationError.RecordIdNotFound(recordId) =>
ErrorResponse.notFound(detail = Some(s"Record Id not found: $recordId"))
case PresentationError.ThreadIdNotFound(thid) =>
ErrorResponse.notFound(detail = Some(s"Thread Id not found: $thid"))
case PresentationError.InvalidFlowStateError(msg) =>
ErrorResponse.badRequest(title = "InvalidFlowState", detail = Some(msg))
case PresentationError.MissingAnoncredPresentationRequest(msg) =>
ErrorResponse.badRequest(title = "Missing Anoncred Presentation Request", detail = Some(msg))
case PresentationError.AnoncredPresentationCreationError(cause) =>
ErrorResponse.badRequest(title = "Error Creating Anoncred Presentation", detail = Some(cause.toString))
case PresentationError.AnoncredPresentationVerificationError(cause) =>
ErrorResponse.badRequest(title = "Error Verifying Prensetation", detail = Some(cause.toString))
case PresentationError.InvalidAnoncredPresentationRequest(msg) =>
ErrorResponse.badRequest(title = "Invalid Anoncred Presentation Request", detail = Some(msg))
case PresentationError.InvalidAnoncredPresentation(msg) =>
ErrorResponse.badRequest(title = "Invalid Anoncred Presentation", detail = Some(msg))
case PresentationError.NotMatchingPresentationCredentialFormat(cause) =>
ErrorResponse.badRequest(
title = "Presentation and Credential Format Not Matching",
detail = Some(cause.toString)
)
case PresentationError.UnexpectedError(msg) =>
ErrorResponse.internalServerError(detail = Some(msg))
case PresentationError.IssuedCredentialNotFoundError(_) =>
ErrorResponse.internalServerError(detail = Some("Issued credential not found"))
case PresentationError.PresentationDecodingError(_) =>
ErrorResponse.internalServerError(detail = Some("Presentation decoding error"))
case PresentationError.HolderBindingError(msg) =>
ErrorResponse.internalServerError(detail = Some(s"Holder binding error: $msg"))
case PresentationError.MissingCredential =>
ErrorResponse.badRequest(
title = "MissingCredential",
detail = Some("The Credential is missing from attachments")
)
case PresentationError.MissingCredentialFormat =>
ErrorResponse.badRequest(
title = "MissingCredentialFormat",
detail = Some("The Credential format is missing from the credential in attachment")
)
case PresentationError.UnsupportedCredentialFormat(format) =>
ErrorResponse.badRequest(
title = "UnsupportedCredentialFormat",
detail = Some(s"The Credential format '$format' is not Unsupported")
)

def toDidCommID(str: String): ZIO[Any, ErrorResponse, org.hyperledger.identus.pollux.core.model.DidCommID] =
ZIO
.fromTry(Try(org.hyperledger.identus.pollux.core.model.DidCommID(str)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import zio.json.*
import zio.json.ast.Json

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

class PresentProofControllerImpl(
presentationService: PresentationService,
Expand Down Expand Up @@ -95,11 +96,7 @@ class PresentProofControllerImpl(
}
}
} yield PresentationStatus.fromDomain(record)

result.mapError {
case e: ConnectionServiceError => e.asInstanceOf[ErrorResponse] // use implicit conversion
case e: PresentationError => PresentProofController.toHttpError(e)
}
result
}

override def getPresentations(paginationInput: PaginationInput, thid: Option[String])(implicit
Expand All @@ -113,7 +110,7 @@ class PresentProofControllerImpl(
records.map(PresentationStatus.fromDomain)
)

result.mapError(PresentProofController.toHttpError)
result
}

override def getPresentation(
Expand All @@ -126,11 +123,7 @@ class PresentProofControllerImpl(
.fromOption(maybeRecord)
.mapError(_ => ErrorResponse.notFound(detail = Some(s"Presentation record not found: $id")))
} yield PresentationStatus.fromDomain(record)

result.mapError {
case e: ErrorResponse => e
case e: PresentationError => PresentProofController.toHttpError(e)
}
result
}

override def updatePresentation(id: UUID, requestPresentationAction: RequestPresentationAction)(implicit
Expand Down Expand Up @@ -173,10 +166,7 @@ class PresentProofControllerImpl(
}
} yield PresentationStatus.fromDomain(record)

result.mapError {
case e: ErrorResponse => e
case e: PresentationError => PresentProofController.toHttpError(e)
}
result
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package org.hyperledger.identus.pollux.schema
import com.dimafeng.testcontainers.PostgreSQLContainer
import org.hyperledger.identus.agent.walletapi.model.Entity
import org.hyperledger.identus.container.util.MigrationAspects.*
import org.hyperledger.identus.pollux.core.model.error.CredentialSchemaGuidNotFoundError
import org.hyperledger.identus.pollux.core.model.schema.`type`.CredentialJsonSchemaType
import org.hyperledger.identus.pollux.core.model.schema.CredentialSchema
import org.hyperledger.identus.pollux.core.service.{
CredentialSchemaGuidNotFoundError,
CredentialSchemaService,
CredentialSchemaServiceImpl
}
import org.hyperledger.identus.pollux.core.service.{CredentialSchemaService, CredentialSchemaServiceImpl}
import org.hyperledger.identus.pollux.sql.repository.JdbcCredentialSchemaRepository
import zio.*
import zio.json.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.hyperledger.identus.pollux.core.service
package org.hyperledger.identus.pollux.core.model.error

import org.hyperledger.identus.pollux.core.model.error.CredentialSchemaError
import org.hyperledger.identus.pollux.core.model.schema.CredentialSchema
import org.hyperledger.identus.pollux.core.model.schema.CredentialSchema.*
import org.hyperledger.identus.shared.models.{Failure, StatusCode}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.hyperledger.identus.pollux.core.service
package org.hyperledger.identus.pollux.core.model.error

import org.hyperledger.identus.pollux.core.model.error.CredentialSchemaError
import org.hyperledger.identus.pollux.core.model.schema.CredentialSchema
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,161 @@
package org.hyperledger.identus.pollux.core.model.error

import org.hyperledger.identus.pollux.core.model.schema.validator.JsonSchemaError
import org.hyperledger.identus.pollux.core.model.DidCommID
import org.hyperledger.identus.pollux.core.service.URIDereferencerError
import org.hyperledger.identus.shared.models.{Failure, StatusCode}

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

object PresentationError {
final case class RecordIdNotFound(recordId: DidCommID) extends PresentationError
final case class ThreadIdNotFound(thid: DidCommID) extends PresentationError
final case class InvalidFlowStateError(msg: String) extends PresentationError
final case class UnexpectedError(msg: String) extends PresentationError
final case class IssuedCredentialNotFoundError(cause: String) extends PresentationError
final case class NotMatchingPresentationCredentialFormat(cause: Throwable) extends PresentationError
final case class PresentationDecodingError(cause: String) extends PresentationError
final case class HolderBindingError(msg: String) extends PresentationError
object MissingCredential extends PresentationError
object MissingCredentialFormat extends PresentationError
final case class UnsupportedCredentialFormat(vcFormat: String) extends PresentationError
final case class InvalidAnoncredPresentationRequest(error: String) extends PresentationError
final case class InvalidAnoncredPresentation(error: String) extends PresentationError
final case class MissingAnoncredPresentationRequest(error: String) extends PresentationError
final case class AnoncredPresentationCreationError(cause: Throwable) extends PresentationError
final case class AnoncredPresentationVerificationError(cause: Throwable) extends PresentationError

// TODO: Remove once PresentationJob is cleaned
final case class UnexpectedError(error: String)
extends PresentationError(
StatusCode.InternalServerError,
error
)

final case class RecordIdNotFound(recordId: DidCommID)
extends PresentationError(
StatusCode.NotFound,
s"Record Id not found: $recordId"
)

final case class ThreadIdNotFound(thid: DidCommID)
extends PresentationError(
StatusCode.NotFound,
s"Thread Id not found: $thid"
)

final case class NoThreadIdFoundInRecord(presentationId: String)
extends PresentationError(
StatusCode.InternalServerError,
s"Presentation record has missing ThreadId for record: $presentationId"
)

final case class InvalidFlowStateError(msg: String)
extends PresentationError(
StatusCode.BadRequest,
msg
)

final case class RequestPresentationHasMultipleAttachment(presentationId: String)
extends PresentationError(
StatusCode.BadRequest,
s"Request Presentation with multi attachments: $presentationId"
)

final case class IssuedCredentialNotFoundError(cause: String)
extends PresentationError(
StatusCode.InternalServerError,
"Issued credential not found"
)

final case class InvalidSchemaURIError(schemaUri: String, error: Throwable)
extends PresentationError(
StatusCode.BadRequest,
s"Invalid Schema Uri: $schemaUri, Error: ${error.getMessage}"
)

final case class InvalidCredentialDefinitionURIError(credentialDefinitionUri: String, error: Throwable)
extends PresentationError(
StatusCode.BadRequest,
s"Invalid Credential Definition Uri: $credentialDefinitionUri, Error: ${error.getMessage}"
)

final case class SchemaURIDereferencingError(error: URIDereferencerError)
extends PresentationError(
error.statusCode,
error.userFacingMessage
)

final case class CredentialDefinitionURIDereferencingError(error: URIDereferencerError)
extends PresentationError(
error.statusCode,
error.userFacingMessage
)

final case class PresentationDecodingError(cause: String)
extends PresentationError(
StatusCode.InternalServerError,
s"Presentation decoding error: $cause"
)

final case class HolderBindingError(msg: String)
extends PresentationError(
StatusCode.InternalServerError,
s"Holder binding error: $msg"
)

object MissingCredential
extends PresentationError(
StatusCode.BadRequest,
s"The Credential is missing from attachments"
)

object MissingCredentialFormat
extends PresentationError(
StatusCode.BadRequest,
s"The Credential format is missing from the credential in attachment"
)

final case class UnsupportedCredentialFormat(vcFormat: String)
extends PresentationError(
StatusCode.BadRequest,
s"The Credential format '$vcFormat' is not Unsupported"
)

final case class InvalidAnoncredPresentationRequest(error: String)
extends PresentationError(
StatusCode.InternalServerError,
error
)

final case class InvalidAnoncredPresentation(error: String)
extends PresentationError(
StatusCode.InternalServerError,
error
)

final case class MissingAnoncredPresentationRequest(error: String)
extends PresentationError(
StatusCode.InternalServerError,
error
)

final case class NotMatchingPresentationCredentialFormat(cause: Throwable)
extends PresentationError(
StatusCode.BadRequest,
s"Presentation and Credential Format Not Matching: ${cause.toString}"
)

final case class AnoncredPresentationCreationError(cause: Throwable)
extends PresentationError(
StatusCode.InternalServerError,
cause.toString
)

final case class AnoncredCredentialProofParsingError(cause: String)
extends PresentationError(
StatusCode.BadRequest,
cause
)

final case class AnoncredPresentationParsingError(cause: JsonSchemaError)
extends PresentationError(
StatusCode.BadRequest,
cause.error
)

final case class AnoncredPresentationVerificationError(cause: Throwable)
extends PresentationError(
StatusCode.BadRequest,
cause.toString
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.hyperledger.identus.pollux.core.service

import org.hyperledger.identus.pollux.core.model.error.CredentialDefinitionServiceError
import org.hyperledger.identus.pollux.core.model.schema.CredentialDefinition
import org.hyperledger.identus.pollux.core.model.schema.CredentialDefinition.*
import org.hyperledger.identus.shared.models.WalletAccessContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ package org.hyperledger.identus.pollux.core.service
import org.hyperledger.identus.agent.walletapi.storage
import org.hyperledger.identus.agent.walletapi.storage.GenericSecretStorage
import org.hyperledger.identus.pollux.anoncreds.{AnoncredLib, AnoncredSchemaDef}
import org.hyperledger.identus.pollux.core.model.error.CredentialSchemaError
import org.hyperledger.identus.pollux.core.model.error.{
CredentialDefinitionCreationError,
CredentialDefinitionGuidNotFoundError,
CredentialDefinitionServiceError,
CredentialDefinitionValidationError,
CredentialSchemaError
}
import org.hyperledger.identus.pollux.core.model.error.CredentialSchemaError.{
CredentialSchemaParsingError,
CredentialSchemaValidationError,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.hyperledger.identus.pollux.core.service

import org.hyperledger.identus.pollux.core.model.error.CredentialSchemaServiceError
import org.hyperledger.identus.pollux.core.model.schema.CredentialSchema
import org.hyperledger.identus.pollux.core.model.schema.CredentialSchema.*
import org.hyperledger.identus.shared.models.WalletAccessContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package org.hyperledger.identus.pollux.core.service

import org.hyperledger.identus.pollux.core.model.error.CredentialSchemaError
import org.hyperledger.identus.pollux.core.model.error.{
CredentialSchemaError,
CredentialSchemaGuidNotFoundError,
CredentialSchemaServiceError,
CredentialSchemaUpdateError,
CredentialSchemaValidationError
}
import org.hyperledger.identus.pollux.core.model.schema.CredentialSchema
import org.hyperledger.identus.pollux.core.model.schema.CredentialSchema.FilteredEntries
import org.hyperledger.identus.pollux.core.repository.CredentialSchemaRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ private class CredentialServiceImpl(
guid: UUID
): UIO[CredentialDefinition] = credentialDefinitionService
.getByGUID(guid)
.mapError(e => CredentialDefinitionServiceError(e.toString))
.orDieAsUnmanagedFailure

private[this] def getCredentialDefinitionPrivatePart(
Expand Down
Loading

0 comments on commit 9ac6e52

Please sign in to comment.