Skip to content

Commit

Permalink
Merge branch 'main' into feat/SDJWT-CompactFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
mineme0110 authored Jun 11, 2024
2 parents d6eb1ae + ede7b77 commit acbf358
Show file tree
Hide file tree
Showing 39 changed files with 1,277 additions and 1,655 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.hyperledger.identus.agent.server.http

import org.hyperledger.identus.api.http.ErrorResponse
import org.hyperledger.identus.shared.models.StatusCode
import org.hyperledger.identus.shared.models.{Failure, StatusCode, UnmanagedFailureException}
import org.log4s.*
import sttp.tapir.*
import sttp.tapir.json.zio.jsonBody
Expand All @@ -12,6 +12,8 @@ import sttp.tapir.server.interceptor.exception.ExceptionHandler
import sttp.tapir.server.interceptor.reject.RejectHandler
import sttp.tapir.server.model.ValuedEndpointOutput

import scala.language.implicitConversions

object CustomServerInterceptors {

private val logger: Logger = getLogger
Expand All @@ -29,18 +31,21 @@ object CustomServerInterceptors {
}

def exceptionHandler[F[_]]: ExceptionHandler[F] = ExceptionHandler.pure[F](ctx =>
defectHandler(
ErrorResponse(
StatusCode.InternalServerError.code,
s"error:InternalServerError",
"Internal Server Error",
Some(
s"An unexpected error occurred when processing the request: " +
s"path=['${ctx.request.showShort}']"
ctx.e match
case UnmanagedFailureException(failure: Failure) => defectHandler(failure)
case e =>
defectHandler(
ErrorResponse(
StatusCode.InternalServerError.code,
s"error:InternalServerError",
"Internal Server Error",
Some(
s"An unexpected error occurred when processing the request: " +
s"path=['${ctx.request.showShort}']"
)
),
Some(ctx.e)
)
),
Some(ctx.e)
)
)

def rejectHandler[F[_]]: RejectHandler[F] = RejectHandler.pure[F](resultFailure =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ object IssueBackgroundJobs extends BackgroundJobsHelper {
IssueCredentialRecord.ProtocolState.CredentialPending,
IssueCredentialRecord.ProtocolState.CredentialGenerated
)
.mapError(err => Throwable(s"Error occurred while getting Issue Credential records: $err"))
_ <- ZIO
.foreachPar(records)(performIssueCredentialExchange)
.withParallelism(config.pollux.issueBgJobProcessingParallelism)
Expand Down Expand Up @@ -222,12 +221,12 @@ object IssueBackgroundJobs extends BackgroundJobsHelper {
) =>
val holderPendingToGeneratedFlow = for {
walletAccessContext <- buildWalletAccessContextLayer(offer.to)
result <- (for {
result <- for {
credentialService <- ZIO.service[CredentialService]
_ <- credentialService
.generateJWTCredentialRequest(id)
.provideSomeLayer(ZLayer.succeed(walletAccessContext))
} yield ()).mapError(e => (walletAccessContext, handleCredentialErrors(e)))
} yield ()
} yield result

holderPendingToGeneratedFlow @@ HolderPendingToGeneratedSuccess.trackSuccess
Expand Down Expand Up @@ -263,12 +262,12 @@ object IssueBackgroundJobs extends BackgroundJobsHelper {
) =>
val holderPendingToGeneratedFlow = for {
walletAccessContext <- buildWalletAccessContextLayer(offer.to)
result <- (for {
result <- for {
credentialService <- ZIO.service[CredentialService]
_ <- credentialService
.generateSDJWTCredentialRequest(id)
.provideSomeLayer(ZLayer.succeed(walletAccessContext))
} yield ()).mapError(e => (walletAccessContext, handleCredentialErrors(e)))
} yield ()
} yield result

holderPendingToGeneratedFlow @@ HolderPendingToGeneratedSuccess.trackSuccess
Expand Down Expand Up @@ -304,12 +303,12 @@ object IssueBackgroundJobs extends BackgroundJobsHelper {
) =>
val holderPendingToGeneratedFlow = for {
walletAccessContext <- buildWalletAccessContextLayer(offer.to)
result <- (for {
result <- for {
credentialService <- ZIO.service[CredentialService]
_ <- credentialService
.generateAnonCredsCredentialRequest(id)
.provideSomeLayer(ZLayer.succeed(walletAccessContext))
} yield ()).mapError(e => (walletAccessContext, handleCredentialErrors(e)))
} yield ()
} yield result

holderPendingToGeneratedFlow @@ HolderPendingToGeneratedSuccess.trackSuccess
Expand Down Expand Up @@ -617,12 +616,6 @@ object IssueBackgroundJobs extends BackgroundJobsHelper {
_ <- credentialService
.reportProcessingFailure(record.id, Some(e.toString))
.provideSomeLayer(ZLayer.succeed(walletAccessContext))
.tapError(err =>
ZIO.logErrorCause(
s"Issue Credential - failed to report processing failure: ${record.id}",
Cause.fail(err)
)
)
} yield ()

}
Expand All @@ -634,9 +627,4 @@ object IssueBackgroundJobs extends BackgroundJobsHelper {

}

private def handleCredentialErrors: PartialFunction[Throwable | CredentialServiceError, CredentialServiceError] = {
case e: CredentialServiceError => e
case t: Throwable => CredentialServiceError.UnexpectedError(t.getMessage())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,10 @@ object PresentBackgroundJobs extends BackgroundJobsHelper {
.attempt(DidCommID(credentialRecordId))
.mapError(_ => PresentationError.UnexpectedError(s"$credentialRecordId is not a valid DidCommID"))
vcSubjectId <- credentialService
.getIssueCredentialRecord(credentialRecordUuid)
.someOrFail(CredentialServiceError.RecordIdNotFound(credentialRecordUuid))
.findById(credentialRecordUuid)
.someOrFail(CredentialServiceError.RecordNotFound(credentialRecordUuid))
.map(_.subjectId)
.someOrFail(
CredentialServiceError.UnexpectedError(s"VC SubjectId not found in credential record: $credentialRecordUuid")
)
.someOrElseZIO(ZIO.dieMessage(s"VC SubjectId not found in credential record: $credentialRecordUuid"))
proverDID <- ZIO
.fromEither(PrismDID.fromString(vcSubjectId))
.mapError(e =>
Expand Down Expand Up @@ -120,12 +118,10 @@ object PresentBackgroundJobs extends BackgroundJobsHelper {
.attempt(DidCommID(credentialRecordId))
.mapError(_ => PresentationError.UnexpectedError(s"$credentialRecordId is not a valid DidCommID"))
vcSubjectId <- credentialService
.getIssueCredentialRecord(credentialRecordUuid)
.someOrFail(CredentialServiceError.RecordIdNotFound(credentialRecordUuid))
.findById(credentialRecordUuid)
.someOrFail(CredentialServiceError.RecordNotFound(credentialRecordUuid))
.map(_.subjectId)
.someOrFail(
CredentialServiceError.UnexpectedError(s"VC SubjectId not found in credential record: $credentialRecordUuid")
)
.someOrElseZIO(ZIO.dieMessage(s"VC SubjectId not found in credential record: $credentialRecordUuid"))
proverDID <- ZIO
.fromEither(PrismDID.fromString(vcSubjectId))
.mapError(e =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ object StatusListJobs extends BackgroundJobsHelper {
updateBitStringEffects = statusListWithCreds.credentials.map { cred =>
if cred.isCanceled then {
val sendMessageEffect = for {
maybeIssueCredentialRecord <- credentialService
.getIssueCredentialRecord(cred.issueCredentialRecordId)
.mapError(_.toThrowable)
maybeIssueCredentialRecord <- credentialService.findById(cred.issueCredentialRecordId)
issueCredentialRecord <- ZIO
.fromOption(maybeIssueCredentialRecord)
.mapError(_ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ object ErrorResponse {
given schema: Schema[ErrorResponse] = Schema.derived

private val CamelCaseSplitRegex: Regex = "(([A-Z]?[a-z]+)|([A-Z]))".r
given failureToErrorResponseConversionZIO[R, A]: Conversion[ZIO[R, Failure, A], ZIO[R, ErrorResponse, A]] = {
effect => effect.mapError { failure => failure }
given failureToErrorResponseConversionZIO[R, A]
: Conversion[ZIO[R, Failure | ErrorResponse, A], ZIO[R, ErrorResponse, A]] = { effect =>
effect.mapError {
case failure: Failure => failure
case e: ErrorResponse => e
}
}

given failureToErrorResponseConversion[R, A]: Conversion[Failure, ErrorResponse] = { failure =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.hyperledger.identus.issue.controller.http.{
IssueCredentialRecord,
IssueCredentialRecordPage
}
import org.hyperledger.identus.pollux.core.model.error.CredentialServiceError
import org.hyperledger.identus.shared.models.WalletAccessContext
import zio.*

Expand All @@ -34,47 +33,3 @@ trait IssueController {
): ZIO[WalletAccessContext, ErrorResponse, IssueCredentialRecord]

}

object IssueController {
def toHttpError(error: CredentialServiceError): ErrorResponse =
error match
case CredentialServiceError.RepositoryError(cause) =>
ErrorResponse.internalServerError(title = "RepositoryError", detail = Some(cause.toString))
case CredentialServiceError.LinkSecretError(cause) =>
ErrorResponse.internalServerError(title = "LinkSecretError", detail = Some(cause.toString))
case CredentialServiceError.RecordIdNotFound(recordId) =>
ErrorResponse.notFound(detail = Some(s"Record Id not found: $recordId"))
case CredentialServiceError.OperationNotExecuted(recordId, info) =>
ErrorResponse.internalServerError(title = "Operation Not Executed", detail = Some(s"${recordId}-${info}"))
case CredentialServiceError.ThreadIdNotFound(thid) =>
ErrorResponse.notFound(detail = Some(s"Thread Id not found: $thid"))
case CredentialServiceError.UnexpectedError(msg) =>
ErrorResponse.internalServerError(detail = Some(msg))
case CredentialServiceError.InvalidFlowStateError(msg) =>
ErrorResponse.badRequest(title = "InvalidFlowState", detail = Some(msg))
case CredentialServiceError.UnsupportedDidFormat(did) =>
ErrorResponse.badRequest("Unsupported DID format", Some(s"The following DID is not supported: ${did}"))
case CredentialServiceError.CreateCredentialPayloadFromRecordError(msg) =>
ErrorResponse.badRequest(title = "Create Credential Payload From Record Error", detail = Some(msg.getMessage))
case CredentialServiceError.CredentialRequestValidationError(msg) =>
ErrorResponse.badRequest(title = "Create Request Validation Error", detail = Some(msg))
case CredentialServiceError.CredentialIdNotDefined(msg) =>
ErrorResponse.badRequest(title = "Credential ID not defined one request", detail = Some(msg.toString))
case CredentialServiceError.CredentialSchemaError(e) =>
ErrorResponse.badRequest(title = "Credential Schema Error", detail = Some(e.message))
case CredentialServiceError.UnsupportedVCClaimsValue(error) =>
ErrorResponse.badRequest(detail = Some(error))
case CredentialServiceError.UnsupportedVCClaimsMediaType(media_type) =>
ErrorResponse.badRequest(detail = Some(s"Unsupported media_type for claim: $media_type"))
case CredentialServiceError.UnsupportedCredentialFormat(format) =>
ErrorResponse.badRequest(detail = Some(s"Unsupported format in claim: $format"))
case CredentialServiceError.MissingCredentialFormat =>
ErrorResponse.badRequest(detail = Some(s"Missing credential format in claim"))
case CredentialServiceError.CredentialDefinitionPrivatePartNotFound(credentialDefinitionId) =>
ErrorResponse.badRequest(detail =
Some(s"Credential Definition (id: $credentialDefinitionId) private part not found")
)
case CredentialServiceError.CredentialDefinitionIdUndefined =>
ErrorResponse.badRequest(detail = Some(s"Credential Definition id undefined"))

}
Loading

0 comments on commit acbf358

Please sign in to comment.