Skip to content

Commit

Permalink
Republish error codes generator lib (#11633)
Browse files Browse the repository at this point in the history
* Republish error codes generator libraries
* Additionally, add kvutils to classpath for generating KVErrors docs

CHANGELOG_BEGIN
CHANGELOG_END

* Use a new annotation (DeprecatedDocs) instead of @deprecated

* Needed for adding descriptions to the error groups docs. Needed for allowing cross-compilation
to 2.12 and 2.13, not possible due to the reflections machinery of extracting
the message from @deprecated
  • Loading branch information
tudor-da authored Nov 10, 2021
1 parent a6f745f commit c5a1f0b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 20 deletions.
25 changes: 22 additions & 3 deletions ledger/error/generator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,44 @@
load(
"//bazel_tools:scala.bzl",
"da_scala_binary",
"da_scala_library",
"da_scala_test_suite",
)
load("@scala_version//:index.bzl", "scala_major_version")

da_scala_binary(
name = "export-error-codes-json-app",
srcs = glob(["src/main/scala/**/*.scala"]),
main_class = "com.daml.error.generator.Main",
srcs = glob(["app/src/main/scala/**/*.scala"]),
main_class = "com.daml.error.generator.app.Main",
resources = glob(["src/main/resources/**/*"]),
scala_deps = [
"@maven//:io_circe_circe_core",
"@maven//:org_typelevel_cats_core",
],
visibility = ["//visibility:public"],
runtime_deps = [
# Add the KVErrors to the classpath so they can be picked up by the generator
"//ledger/participant-state/kvutils",
],
deps = [
"//ledger/error",
"//ledger/error/generator:lib",
],
)

da_scala_library(
name = "lib",
srcs = glob(["lib/src/main/scala/**/*.scala"]),
tags = ["maven_coordinates=com.daml:error-generator-lib:__VERSION__"],
versioned_scala_deps = {
"2.12": ["@maven//:org_scala_lang_modules_scala_collection_compat"],
},
visibility = ["//visibility:public"],
deps = [
"//ledger/error",
"@maven//:org_reflections_reflections",
],
) if scala_major_version == "2.13" else None
)

da_scala_test_suite(
name = "export-error-codes-json-tests",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.error.generator
package com.daml.error.generator.app

import com.daml.error.Grouping
import com.daml.error.generator.{ErrorCodeDocumentationGenerator, ErrorDocItem, GroupDocItem}
import io.circe.Encoder
import io.circe.syntax._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package com.daml.error.generator

import java.lang.reflect.Modifier

import com.daml.error.{Deprecation, ErrorCode, ErrorGroup, Explanation, Resolution}
import com.daml.error.{DeprecatedDocs, Deprecation, ErrorCode, ErrorGroup, Explanation, Resolution}
import com.daml.error.generator.ErrorCodeDocumentationGenerator.{
acceptedTypeNames,
deprecatedDocsTypeName,
deprecatedTypeName,
explanationTypeName,
resolutionTypeName,
Expand Down Expand Up @@ -163,13 +163,17 @@ class ErrorCodeDocumentationGenerator(prefixes: Array[String] = Array("com.daml"

val doc = annotations.foldLeft(GetAnnotationsState(None, None, None)) {
case (state, annotation) =>
if (isAnnotation(annotation, deprecatedTypeName))
update(state, updatedDeprecation = Some(parseDeprecatedAnnotationValue(annotation.tree)))
if (isAnnotation(annotation, deprecatedDocsTypeName))
update(state, updatedDeprecation = Some(parseAnnotationValue(annotation.tree)))
else if (isAnnotation(annotation, explanationTypeName))
update(state, updatedExplanation = Some(parseAnnotationValue(annotation.tree)))
else if (isAnnotation(annotation, resolutionTypeName))
update(state, updatedResolution = Some(parseAnnotationValue(annotation.tree)))
else
else if (isAnnotation(annotation, deprecatedTypeName)) {
// Ignore @deprecate annotations
// TODO Scala 2.13: Use these annotations to extract the docs description instead of the redundant [[DeprecatedDocs]]
state
} else
sys.error(
s"Unexpected annotation detected (${annotations.map(annotationTypeName)} but the only supported ones are $acceptedTypeNames)."
)
Expand All @@ -178,16 +182,6 @@ class ErrorCodeDocumentationGenerator(prefixes: Array[String] = Array("com.daml"
ErrorDocumentationAnnotations(doc.deprecation, doc.explanation, doc.resolution)
}

private def parseDeprecatedAnnotationValue(tree: ru.Tree): String =
tree
.children(1)
.asInstanceOf[ru.NamedArg]
.children(1)
.asInstanceOf[ru.Literal]
.value
.value
.asInstanceOf[String]

@SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf"))
private def parseAnnotationValue(tree: ru.Tree): String = {
try {
Expand Down Expand Up @@ -216,8 +210,10 @@ private object ErrorCodeDocumentationGenerator {
private val runtimeMirror: ru.Mirror = ru.runtimeMirror(getClass.getClassLoader)

private val deprecatedTypeName = classOf[deprecated].getTypeName.replace("scala.", "")
private val deprecatedDocsTypeName = classOf[DeprecatedDocs].getTypeName.replace("$", ".")
private val explanationTypeName = classOf[Explanation].getTypeName.replace("$", ".")
private val resolutionTypeName = classOf[Resolution].getTypeName.replace("$", ".")

private val acceptedTypeNames = Set(deprecatedTypeName, explanationTypeName, resolutionTypeName)
private val acceptedTypeNames =
Set(deprecatedTypeName, deprecatedDocsTypeName, explanationTypeName, resolutionTypeName)
}
1 change: 1 addition & 0 deletions ledger/error/src/main/scala/com/daml/error/ErrorCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,4 @@ object ErrorCode {
case class Deprecation(deprecation: String) extends StaticAnnotation
case class Explanation(explanation: String) extends StaticAnnotation
case class Resolution(resolution: String) extends StaticAnnotation
case class DeprecatedDocs(description: String) extends StaticAnnotation
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package com.daml.ledger.participant.state.kvutils.errors
import java.time.Instant
import com.daml.error.{
ContextualizedErrorLogger,
DeprecatedDocs,
ErrorCategory,
ErrorCode,
ErrorGroup,
Expand Down Expand Up @@ -374,6 +375,9 @@ object KVErrors extends LedgerApiErrorGroup {
"The ledger time of the submission violated some constraint on the ledger time."
)
@Resolution("Retry the transaction submission.")
@DeprecatedDocs(
"It was produced by submissions batching and it's not produced anymore by pre-execution."
)
@deprecated(
"It was produced by submissions batching and it's not produced anymore by pre-execution."
)
Expand All @@ -400,6 +404,9 @@ object KVErrors extends LedgerApiErrorGroup {
"Check that all the informee party identifiers are correct, allocate all the informee parties, " +
"request their allocation or wait for them to be allocated before retrying the transactiomn submission."
)
@DeprecatedDocs(
"Corresponds to transaction submission rejections that are not produced anymore."
)
@deprecated("Corresponds to transaction submission rejections that are not produced anymore.")
object PartyNotKnownOnLedger
extends ErrorCode(
Expand All @@ -422,6 +429,9 @@ object KVErrors extends LedgerApiErrorGroup {

@Explanation("An invalid transaction submission was not detected by the participant.")
@Resolution("Contact support.")
@DeprecatedDocs(
"Corresponds to transaction submission rejections that are not produced anymore."
)
@deprecated("Corresponds to transaction submission rejections that are not produced anymore.")
object Disputed
extends ErrorCode(
Expand All @@ -444,6 +454,9 @@ object KVErrors extends LedgerApiErrorGroup {
"without an archived contract as an input, or the transaction submission may be retried " +
"to load the up-to-date value of a contract key."
)
@DeprecatedDocs(
"Corresponds to transaction submission rejections that are not produced anymore."
)
@deprecated("Corresponds to transaction submission rejections that are not produced anymore.")
object Inconsistent
extends ErrorCode(
Expand Down
2 changes: 2 additions & 0 deletions release/artifacts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,5 @@
type: jar-scala
- target: //ledger/error:error
type: jar-scala
- target: //ledger/error/generator:lib
type: jar-scala

0 comments on commit c5a1f0b

Please sign in to comment.