Refactoring for Canton compatibility on PackageServiceErrors (#11812)

* Refactoring for Canton compatibility on PackageServiceErrors
* ErrorResource.all is public for accessibility from Canton

CHANGELOG_BEGIN
CHANGELOG_END

* Keep PackageServiceError object
This commit is contained in:
tudor-da 2021-11-22 20:14:03 +01:00 committed by GitHub
parent c4b6a17da1
commit 1bfcbc978b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 21 deletions

View File

@ -15,8 +15,7 @@ trait ErrorResource {
object ErrorResource {
private lazy val all =
Seq(ContractId, ContractKey, DalfPackage, LedgerId, CommandId, TransactionId, Party)
val all = Seq(ContractId, ContractKey, DalfPackage, LedgerId, CommandId, TransactionId, Party)
def fromString(str: String): Option[ErrorResource] = all.find(_.asString == str)

View File

@ -10,11 +10,14 @@ import com.daml.lf.data.Ref.PackageId
import com.daml.lf.engine.Error
import com.daml.lf.{VersionRange, language, validation}
abstract class PackageServiceError(
trait PackageServiceError extends BaseError
abstract class LoggingPackageServiceError(
override val cause: String,
override val throwableO: Option[Throwable] = None,
)(implicit override val code: ErrorCode)
extends BaseError.Impl(cause, throwableO) {
extends BaseError.Impl(cause, throwableO)
with PackageServiceError {
final override def logOnCreation: Boolean = true
}
@ -31,7 +34,7 @@ object PackageServiceError extends LedgerApiErrors.PackageServiceErrorGroup {
) {
final case class Error(reason: String)(implicit
val loggingContext: ContextualizedErrorLogger
) extends PackageServiceError(
) extends LoggingPackageServiceError(
cause = "Dar file name is invalid"
)
}
@ -42,7 +45,7 @@ object PackageServiceError extends LedgerApiErrors.PackageServiceErrorGroup {
extends ErrorCode(id = "INVALID_DAR", ErrorCategory.InvalidIndependentOfSystemState) {
final case class Error(entries: Seq[String], throwable: Throwable)(implicit
val loggingContext: ContextualizedErrorLogger
) extends PackageServiceError(
) extends LoggingPackageServiceError(
cause = "Dar file is corrupt",
throwableO = Some(throwable),
)
@ -53,7 +56,7 @@ object PackageServiceError extends LedgerApiErrors.PackageServiceErrorGroup {
extends ErrorCode(id = "INVALID_ZIP_ENTRY", ErrorCategory.InvalidIndependentOfSystemState) {
final case class Error(name: String, entries: Seq[String])(implicit
val loggingContext: ContextualizedErrorLogger
) extends PackageServiceError(
) extends LoggingPackageServiceError(
cause = "Dar zip file is corrupt"
)
}
@ -69,7 +72,7 @@ object PackageServiceError extends LedgerApiErrors.PackageServiceErrorGroup {
) {
final case class Error(entries: Seq[String])(implicit
val loggingContext: ContextualizedErrorLogger
) extends PackageServiceError(
) extends LoggingPackageServiceError(
cause = "Unsupported legacy Dar zip file"
)
}
@ -80,7 +83,7 @@ object PackageServiceError extends LedgerApiErrors.PackageServiceErrorGroup {
extends ErrorCode(id = "ZIP_BOMB", ErrorCategory.InvalidIndependentOfSystemState) {
final case class Error(msg: String)(implicit
val loggingContext: ContextualizedErrorLogger
) extends PackageServiceError(
) extends LoggingPackageServiceError(
cause = "Dar zip file seems to be a zip bomb."
)
}
@ -93,7 +96,7 @@ object PackageServiceError extends LedgerApiErrors.PackageServiceErrorGroup {
extends ErrorCode(id = "DAR_PARSE_ERROR", ErrorCategory.InvalidIndependentOfSystemState) {
final case class Error(reason: String)(implicit
val loggingContext: ContextualizedErrorLogger
) extends PackageServiceError(
) extends LoggingPackageServiceError(
cause = "Failed to parse the dar file content."
)
}
@ -109,22 +112,22 @@ object PackageServiceError extends LedgerApiErrors.PackageServiceErrorGroup {
) {
final case class Validation(nameOfFunc: String, msg: String, detailMsg: String = "")(implicit
val loggingContext: ContextualizedErrorLogger
) extends PackageServiceError(
) extends LoggingPackageServiceError(
cause = "Internal package validation error."
)
final case class Error(missing: Set[PackageId])(implicit
val loggingContext: ContextualizedErrorLogger
) extends PackageServiceError(
) extends LoggingPackageServiceError(
cause = "Failed to resolve package ids locally."
)
final case class Generic(reason: String)(implicit
val loggingContext: ContextualizedErrorLogger
) extends PackageServiceError(
) extends LoggingPackageServiceError(
cause = "Generic error (please check the reason string)."
)
final case class Unhandled(throwable: Throwable)(implicit
val loggingContext: ContextualizedErrorLogger
) extends PackageServiceError(
) extends LoggingPackageServiceError(
cause = "Failed with an unknown error cause",
throwableO = Some(throwable),
)
@ -133,7 +136,7 @@ object PackageServiceError extends LedgerApiErrors.PackageServiceErrorGroup {
object Validation {
def handleLfArchiveError(
lfArchiveError: LfArchiveError
)(implicit contextualizedErrorLogger: ContextualizedErrorLogger): PackageServiceError =
)(implicit contextualizedErrorLogger: ContextualizedErrorLogger): LoggingPackageServiceError =
lfArchiveError match {
case LfArchiveError.InvalidDar(entries, cause) =>
PackageServiceError.Reading.InvalidDar
@ -153,7 +156,7 @@ object PackageServiceError extends LedgerApiErrors.PackageServiceErrorGroup {
def handleLfEnginePackageError(err: Error.Package.Error)(implicit
loggingContext: ContextualizedErrorLogger
): PackageServiceError = err match {
): LoggingPackageServiceError = err match {
case Error.Package.Internal(nameOfFunc, msg) =>
PackageServiceError.InternalError.Validation(nameOfFunc, msg)
case Error.Package.Validation(validationError) =>
@ -180,7 +183,7 @@ object PackageServiceError extends LedgerApiErrors.PackageServiceErrorGroup {
) {
final case class Error(validationError: validation.ValidationError)(implicit
val loggingContext: ContextualizedErrorLogger
) extends PackageServiceError(
) extends LoggingPackageServiceError(
cause = "Package validation failed."
)
}
@ -191,7 +194,7 @@ object PackageServiceError extends LedgerApiErrors.PackageServiceErrorGroup {
allowedLanguageVersions: VersionRange[language.LanguageVersion],
)(implicit
val loggingContext: ContextualizedErrorLogger
) extends PackageServiceError(
) extends LoggingPackageServiceError(
cause = LedgerApiErrors.CommandExecution.Package.AllowedLanguageVersions
.buildCause(packageId, languageVersion, allowedLanguageVersions)
)(
@ -212,7 +215,7 @@ object PackageServiceError extends LedgerApiErrors.PackageServiceErrorGroup {
missingDependencies: Set[Ref.PackageId],
)(implicit
val loggingContext: ContextualizedErrorLogger
) extends PackageServiceError(
) extends LoggingPackageServiceError(
cause =
"The set of packages in the dar is not self-consistent and is missing dependencies"
)

View File

@ -9,7 +9,7 @@ import akka.stream.Materializer
import akka.stream.scaladsl.Source
import com.daml.api.util.TimestampConversion
import com.daml.daml_lf_dev.DamlLf.Archive
import com.daml.error.definitions.PackageServiceError
import com.daml.error.definitions.LoggingPackageServiceError
import com.daml.error.definitions.PackageServiceError.Validation
import com.daml.error.{
ContextualizedErrorLogger,
@ -145,7 +145,7 @@ private[apiserver] final class ApiPackageManagementService private (
}
private implicit class ErrorValidations[E, R](result: Either[E, R]) {
def handleError(toSelfServiceErrorCode: E => PackageServiceError): Try[R] =
def handleError(toSelfServiceErrorCode: E => LoggingPackageServiceError): Try[R] =
result.left.map { err =>
toSelfServiceErrorCode(err).asGrpcError
}.toTry