Skip to content

Commit

Permalink
Refactoring for Canton compatibility on PackageServiceErrors (#11812)
Browse files Browse the repository at this point in the history
* Refactoring for Canton compatibility on PackageServiceErrors
* ErrorResource.all is public for accessibility from Canton

CHANGELOG_BEGIN
CHANGELOG_END

* Keep PackageServiceError object
  • Loading branch information
tudor-da authored Nov 22, 2021
1 parent c4b6a17 commit 1bfcbc9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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"
)
}
Expand All @@ -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),
)
Expand All @@ -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"
)
}
Expand All @@ -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"
)
}
Expand All @@ -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."
)
}
Expand All @@ -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."
)
}
Expand All @@ -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),
)
Expand All @@ -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
Expand All @@ -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) =>
Expand All @@ -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."
)
}
Expand All @@ -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)
)(
Expand All @@ -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"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1bfcbc9

Please sign in to comment.