Skip to content

Commit

Permalink
[DPP-417][DPP-613] Adopt self-service error codes ApiTransactionService
Browse files Browse the repository at this point in the history
CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
pbatko-da committed Oct 13, 2021
1 parent b738988 commit 526659e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ object ErrorResource {
object ContractKey extends ErrorResource {
def asString: String = "CONTRACT_KEY"
}
object TransactionId extends ErrorResource {
def asString: String = "TRANSACTION_ID"
}
object DalfPackage extends ErrorResource {
def asString: String = "PACKAGE"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,23 @@ object LedgerApiErrors extends LedgerApiErrorGroup {
)
}

@Explanation(
"""TODO: Explanations needs yet to be filled out"""
)
@Resolution("TODO: Resolution needs yet to be filled out")
object TransactionNotFound
extends ErrorCode(
id = "TRANSACTION_NOT_FOUND",
ErrorCategory.InvalidGivenCurrentSystemStateResourceMissing,
) {

case class Reject(transactionId: String)(implicit loggingContext: ErrorCodeLoggingContext)
extends LoggingTransactionErrorImpl(cause = "Transaction not found, or not visible.") {
override def resources: Seq[(ErrorResource, String)] = Seq(
(ErrorResource.ContractKey, transactionId)
)
}
}
}

@Explanation("""This error occurs if the Daml transaction fails due to an authorization error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import com.daml.logging.entries.LoggingEntries
import com.daml.logging.{ContextualizedLogger, LoggingContext}
import com.daml.metrics.Metrics
import com.daml.platform.apiserver.ErrorCodesVersionSwitcher
import com.daml.platform.apiserver.services.transaction.ApiTransactionService._
import com.daml.platform.apiserver.services.{StreamMetrics, logging}
import com.daml.platform.server.api.services.domain.TransactionService
import com.daml.platform.server.api.services.grpc.GrpcTransactionService
Expand All @@ -59,14 +58,6 @@ private[apiserver] object ApiTransactionService {
ledgerId,
PartyNameChecker.AllowAllParties,
)

@throws[StatusRuntimeException]
private def getOrElseThrowNotFound[A](a: Option[A]): A =
a.getOrElse(
throw Status.NOT_FOUND
.withDescription("Transaction not found, or not visible.")
.asRuntimeException()
)
}

private[apiserver] final class ApiTransactionService private (
Expand Down Expand Up @@ -188,8 +179,13 @@ private[apiserver] final class ApiTransactionService private (
.fromString(request.eventId.unwrap)
.fold(
err =>
Future.failed[GetFlatTransactionResponse](
Status.NOT_FOUND.withDescription(s"invalid eventId: $err").asRuntimeException()
errorsVersionsSwitcher.chooseAsFailedFuture[GetFlatTransactionResponse](
v1 = Status.NOT_FOUND.withDescription(s"invalid eventId: $err").asRuntimeException(),
v2 = LedgerApiErrors.CommandValidation.InvalidArgument
.Reject(s"invalid eventId: $err")(
new DamlErrorCodeLoggingContext(logger, loggingContext, None)
)
.asGrpcError,
),
eventId =>
lookUpFlatByTransactionId(
Expand Down Expand Up @@ -221,15 +217,32 @@ private[apiserver] final class ApiTransactionService private (
): Future[GetTransactionResponse] =
transactionsService
.getTransactionTreeById(transactionId, requestingParties)
.map(getOrElseThrowNotFound)
.map(getOrElseThrowNotFound(transactionId))

private def lookUpFlatByTransactionId(
transactionId: TransactionId,
requestingParties: Set[Party],
): Future[GetFlatTransactionResponse] =
): Future[GetFlatTransactionResponse] = {
transactionsService
.getTransactionById(transactionId, requestingParties)
.map(getOrElseThrowNotFound)
.map(getOrElseThrowNotFound(transactionId))
}

@throws[StatusRuntimeException]
private def getOrElseThrowNotFound[A](transactionId: TransactionId)(a: Option[A]): A =
a.getOrElse {
val exception = errorsVersionsSwitcher.choose(
v1 = Status.NOT_FOUND
.withDescription("Transaction not found, or not visible.")
.asRuntimeException(),
v2 = LedgerApiErrors.InterpreterErrors.LookupErrors.TransactionNotFound
.Reject(
transactionId = transactionId.unwrap
)(new DamlErrorCodeLoggingContext(logger, loggingContext, None))
.asGrpcError,
)
throw exception
}

private def transactionsLoggable(transactions: GetTransactionsResponse): String =
s"Responding with transactions: ${transactions.transactions.toList
Expand Down

0 comments on commit 526659e

Please sign in to comment.