From 26653d4fafa92b4b291ac463e012382d3418b0bb Mon Sep 17 00:00:00 2001 From: Tudor Voicu Date: Tue, 2 Nov 2021 11:48:33 +0100 Subject: [PATCH] ErrorCodeDocumentationGenerator uses multiple prefixes for scanning classpaths * Removed ProtoDeserializationErrors (not needed at the moment) CHANGELOG_BEGIN CHANGELOG_END --- .../daml/error/definitions/ErrorGroups.scala | 2 - .../ProtoDeserializationError.scala | 61 ------------------- .../ErrorCodeDocumentationGenerator.scala | 4 +- .../scala/com/daml/error/generator/Main.scala | 2 +- .../ErrorCodeDocumentationGeneratorSpec.scala | 4 +- 5 files changed, 6 insertions(+), 67 deletions(-) delete mode 100644 ledger/error/src/main/scala/com/daml/error/definitions/ProtoDeserializationError.scala diff --git a/ledger/error/src/main/scala/com/daml/error/definitions/ErrorGroups.scala b/ledger/error/src/main/scala/com/daml/error/definitions/ErrorGroups.scala index 12cd54ca3441..c883d53959b3 100644 --- a/ledger/error/src/main/scala/com/daml/error/definitions/ErrorGroups.scala +++ b/ledger/error/src/main/scala/com/daml/error/definitions/ErrorGroups.scala @@ -9,8 +9,6 @@ object ErrorGroups { private implicit val errorClass: ErrorClass = ErrorClass.root() - abstract class ProtoDeserializationErrorGroup extends ErrorGroup - object ParticipantErrorGroup extends ErrorGroup() { abstract class IndexErrorGroup extends ErrorGroup() { abstract class DatabaseErrorGroup extends ErrorGroup() diff --git a/ledger/error/src/main/scala/com/daml/error/definitions/ProtoDeserializationError.scala b/ledger/error/src/main/scala/com/daml/error/definitions/ProtoDeserializationError.scala deleted file mode 100644 index 3a14edb8040b..000000000000 --- a/ledger/error/src/main/scala/com/daml/error/definitions/ProtoDeserializationError.scala +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.daml.error.definitions - -import com.daml.error._ -import com.daml.error.definitions.ErrorGroups.ProtoDeserializationErrorGroup -import com.daml.error.definitions.ProtoDeserializationError.ProtoDeserializationFailure -import com.google.protobuf.InvalidProtocolBufferException - -trait ProtoDeserializationError extends Product with Serializable { - def toAdminError(implicit - loggingContext: ContextualizedErrorLogger - ): BaseError = - ProtoDeserializationFailure.Wrap(this) -} -object ProtoDeserializationError extends ProtoDeserializationErrorGroup { - final case class BufferException(error: InvalidProtocolBufferException) - extends ProtoDeserializationError - // TODO error codes: Adapt, if necessary, the CryptoDeserializationError - // final case class CryptoDeserializationError(error: DeserializationError) extends ProtoDeserializationError - final case class TransactionDeserialization(message: String) extends ProtoDeserializationError - final case class ValueDeserializationError(field: String, message: String) - extends ProtoDeserializationError - final case class StringConversionError(error: String) extends ProtoDeserializationError - final case class UnrecognizedField(error: String) extends ProtoDeserializationError - final case class UnrecognizedEnum(field: String, value: Int) extends ProtoDeserializationError - final case class FieldNotSet(field: String) extends ProtoDeserializationError - final case class NotImplementedYet(className: String) extends ProtoDeserializationError - final case class TimestampConversionError(message: String) extends ProtoDeserializationError - final case class TimeModelConversionError(message: String) extends ProtoDeserializationError - final case class ValueConversionError(field: String, error: String) - extends ProtoDeserializationError - final case class SubmissionIdConversionError(message: String) extends ProtoDeserializationError - final case class InvariantViolation(error: String) extends ProtoDeserializationError - final case class UnknownGrpcCodeError(error: String) extends ProtoDeserializationError - final case class OtherError(error: String) extends ProtoDeserializationError - - /** Common Deserialization error code - * - * USE THIS ERROR CODE ONLY WITHIN A GRPC SERVICE, PARSING THE INITIAL REQUEST. - * Don't used it for something like transaction processing or reading from the database. - */ - @Explanation( - """This error indicates that an incoming administrative command could not be processed due to a malformed message.""" - ) - @Resolution("Inspect the error details and correct your application") - object ProtoDeserializationFailure - extends ErrorCode( - id = "PROTO_DESERIALIZATION_FAILURE", - ErrorCategory.InvalidIndependentOfSystemState, - ) { - case class Wrap(reason: ProtoDeserializationError)(implicit - val loggingContext: ContextualizedErrorLogger - ) extends BaseError.Impl( - cause = "Deserialization of protobuf message failed" - ) - with BaseError - } - -} diff --git a/ledger/error/src/main/scala/com/daml/error/generator/ErrorCodeDocumentationGenerator.scala b/ledger/error/src/main/scala/com/daml/error/generator/ErrorCodeDocumentationGenerator.scala index 24bb8a2baf78..3c2509cdcfa6 100644 --- a/ledger/error/src/main/scala/com/daml/error/generator/ErrorCodeDocumentationGenerator.scala +++ b/ledger/error/src/main/scala/com/daml/error/generator/ErrorCodeDocumentationGenerator.scala @@ -23,7 +23,7 @@ import scala.jdk.CollectionConverters._ * * @param prefix The classpath prefix that should be scanned for finding subtypes of [[ErrorCode]]. */ -case class ErrorCodeDocumentationGenerator(prefix: String = "com.daml") { +class ErrorCodeDocumentationGenerator(prefixes: Array[String] = Array("com.daml")) { def getDocItems: Seq[DocItem] = { val errorCodes = getErrorCodeInstances @@ -40,7 +40,7 @@ case class ErrorCodeDocumentationGenerator(prefix: String = "com.daml") { } private def getErrorCodeInstances: Seq[ErrorCode] = - new Reflections(prefix) + new Reflections(prefixes) .getSubTypesOf(classOf[ErrorCode]) .asScala .view diff --git a/ledger/error/src/main/scala/com/daml/error/generator/Main.scala b/ledger/error/src/main/scala/com/daml/error/generator/Main.scala index ee3f0bb87fb7..99bb8e40f90f 100644 --- a/ledger/error/src/main/scala/com/daml/error/generator/Main.scala +++ b/ledger/error/src/main/scala/com/daml/error/generator/Main.scala @@ -39,7 +39,7 @@ object Main { Encoder.forProduct1("errorCodes")(i => (i.errorCodes)) def main(args: Array[String]): Unit = { - val errorCodes = ErrorCodeDocumentationGenerator().getDocItems + val errorCodes = new ErrorCodeDocumentationGenerator().getDocItems val outputFile = Paths.get(args(0)) val output = Output(errorCodes) val outputText: String = output.asJson.spaces2 diff --git a/ledger/error/src/test/suite/scala/com/daml/error/generator/ErrorCodeDocumentationGeneratorSpec.scala b/ledger/error/src/test/suite/scala/com/daml/error/generator/ErrorCodeDocumentationGeneratorSpec.scala index 1c9492907270..76e525f1f130 100644 --- a/ledger/error/src/test/suite/scala/com/daml/error/generator/ErrorCodeDocumentationGeneratorSpec.scala +++ b/ledger/error/src/test/suite/scala/com/daml/error/generator/ErrorCodeDocumentationGeneratorSpec.scala @@ -16,7 +16,9 @@ class ErrorCodeDocumentationGeneratorSpec extends AnyFlatSpec with Matchers { private val className = ErrorCodeDocumentationGenerator.getClass.getSimpleName // Scan errors from the utils test package - private val generator = ErrorCodeDocumentationGenerator("com.daml.error.utils.testpackage") + private val generator = new ErrorCodeDocumentationGenerator( + Array("com.daml.error.utils.testpackage") + ) s"$className.getDocItems" should "return the correct doc items from the error classes" in { val actualDocItems = generator.getDocItems