Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DPP-673][Self-service error-codes] Generate error code directory compatible with both pdf and html docs #11794

Merged
merged 10 commits into from
Nov 23, 2021
Prev Previous commit
Next Next commit
Revert some renames
  • Loading branch information
pbatko-da committed Nov 22, 2021
commit 9452002f65ba5ad8a8022ad6debfbbe249afa2f4
2 changes: 1 addition & 1 deletion ledger/error/generator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ da_scala_binary(
da_scala_binary(
name = "generate-docs-error-code-inventory-app",
srcs = glob(["app/src/main/scala/**/*.scala"]),
main_class = "com.daml.error.generator.app.ErrorCodeInventoryDocsGen_App",
main_class = "com.daml.error.generator.app.ErrorCodeInventoryDocsGenApp",
resources = glob(["src/main/resources/**/*"]),
scala_deps = [
"@maven//:io_circe_circe_core",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ package com.daml.error.generator.app

import java.nio.file.{Files, Paths, StandardOpenOption}

import com.daml.error.{ErrorGroupPath, ErrorGroupSegment}
import com.daml.error.{ErrorClass, Grouping}
import com.daml.error.generator.{ErrorCodeDocumentationGenerator, ErrorDocItem, GroupDocItem}

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

/** Generates error codes inventory as a reStructuredText
*/
object ErrorCodeInventoryDocsGen_App {
object ErrorCodeInventoryDocsGenApp {

def main(args: Array[String]): Unit = {
val text = {
val (errorDocItems, groupDocItems): (Seq[ErrorDocItem], Seq[GroupDocItem]) =
new ErrorCodeDocumentationGenerator().getDocItems

val groupSegmentsToExplanationMap: Map[List[ErrorGroupSegment], Option[String]] =
val groupSegmentsToExplanationMap: Map[List[Grouping], Option[String]] =
groupDocItems.map { groupDocItem: GroupDocItem =>
groupDocItem.errorGroupPath.segments -> groupDocItem.explanation.map(_.explanation)
}.toMap

val errorCodes: Seq[ErrorCodeValue] = errorDocItems.map { (errorDocItem: ErrorDocItem) =>
ErrorCodeValue(
category = errorDocItem.category,
errorGroupPath = errorDocItem.errorGroupPath,
errorGroupPath = errorDocItem.hierarchicalGrouping,
conveyance = errorDocItem.conveyance.getOrElse("").replace('\n', ' '),
code = errorDocItem.code,
deprecationO = errorDocItem.deprecation.map(_.deprecation.replace('\n', ' ')),
Expand Down Expand Up @@ -62,7 +62,7 @@ object ErrorCodeInventoryDocsGen_App {

case class ErrorCodeValue(
code: String,
errorGroupPath: ErrorGroupPath,
errorGroupPath: ErrorClass,
category: String,
explanation: String,
resolution: String,
Expand All @@ -73,8 +73,8 @@ case class ErrorCodeValue(
class ErrorGroupTree(
val name: String,
val explanation: Option[String] = None,
children: mutable.Map[ErrorGroupSegment, ErrorGroupTree] =
new mutable.HashMap[ErrorGroupSegment, ErrorGroupTree](),
children: mutable.Map[Grouping, ErrorGroupTree] =
new mutable.HashMap[Grouping, ErrorGroupTree](),
errorCodes: mutable.Map[String, ErrorCodeValue] = new mutable.HashMap[String, ErrorCodeValue](),
) {

Expand All @@ -88,7 +88,7 @@ class ErrorGroupTree(

def insertErrorCode(
errorCode: ErrorCodeValue,
getExplanation: (List[ErrorGroupSegment]) => Option[String],
getExplanation: (List[Grouping]) => Option[String],
): Unit = {
insert(
remaining = errorCode.errorGroupPath.segments,
Expand All @@ -99,10 +99,10 @@ class ErrorGroupTree(
}

private def insert(
remaining: List[ErrorGroupSegment],
remaining: List[Grouping],
errorCode: ErrorCodeValue,
path: List[ErrorGroupSegment],
getExplanation: (List[ErrorGroupSegment]) => Option[String],
path: List[Grouping],
getExplanation: (List[Grouping]) => Option[String],
): Unit = {

remaining match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package com.daml.error.generator.app

import com.daml.error.ErrorGroupSegment
import com.daml.error.Grouping
import com.daml.error.generator.{ErrorCodeDocumentationGenerator, ErrorDocItem, GroupDocItem}
import io.circe.Encoder
import io.circe.syntax._
Expand All @@ -16,11 +16,11 @@ object Main {

case class Output(errorCodes: Seq[ErrorDocItem], groups: Seq[GroupDocItem])

implicit val groupingEncode: Encoder[ErrorGroupSegment] =
implicit val groupingEncode: Encoder[Grouping] =
Encoder.forProduct2(
"docName",
"className",
)((grouping: ErrorGroupSegment) =>
)((grouping: Grouping) =>
(
grouping.docName,
grouping.fullClassName,
Expand All @@ -39,9 +39,9 @@ object Main {
"resolution",
)(i =>
(
i.fullClassName,
i.className,
i.category,
i.errorGroupPath.segments,
i.hierarchicalGrouping.segments,
i.conveyance,
i.code,
i.deprecation.fold("")(_.deprecation),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class ErrorCodeDocumentationGenerator(prefixes: Array[String] = Array("com.daml"
getErrorDocumentationAnnotations(error)

ErrorDocItem(
fullClassName = error.getClass.getName,
className = error.getClass.getName,
category = simpleClassName(error.category),
errorGroupPath = error.errorGroupPath,
hierarchicalGrouping = error.errorGroupPath,
conveyance = error.errorConveyanceDocString,
code = error.id,
deprecation = deprecation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@

package com.daml.error.generator

import com.daml.error.{Deprecation, ErrorGroupPath, Explanation, Resolution}
import com.daml.error.{Deprecation, ErrorClass, Explanation, Resolution}

/** Contains error presentation data to be used for documentation rendering on the website.
*
* @param fullClassName The error class name (see [[com.daml.error.ErrorCode]]).
* @param className The error class name (see [[com.daml.error.ErrorCode]]).
* @param category The error code category (see [[com.daml.error.ErrorCategory]]).
* @param errorGroupPath The hierarchical code grouping
* (see [[com.daml.error.ErrorGroupPath]] and [[com.daml.error.ErrorGroup]]).
* @param hierarchicalGrouping The hierarchical code grouping
* (see [[com.daml.error.ErrorClass]] and [[com.daml.error.ErrorGroup]]).
* @param conveyance Provides a statement about the form this error will be returned to the user.
* @param code The error identifier.
* @param explanation The detailed error explanation.
* @param resolution The suggested error resolution.
*/
case class ErrorDocItem(
fullClassName: String, // TODO error codes: Rename to `errorCodeName` or `errorCodeClassName` to prevent confusion
className: String, // TODO error codes: Rename to `errorCodeName` or `errorCodeClassName` to prevent confusion
category: String,
errorGroupPath: ErrorGroupPath,
hierarchicalGrouping: ErrorClass,
conveyance: Option[String],
code: String,
deprecation: Option[Deprecation],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package com.daml.error.generator

import com.daml.error.{ErrorGroupPath, Explanation}
import com.daml.error.{ErrorClass, Explanation}

/** Contains error presentation data to be used for documentation rendering on the website.
*
Expand All @@ -14,5 +14,5 @@ import com.daml.error.{ErrorGroupPath, Explanation}
case class GroupDocItem(
fullClassName: String,
explanation: Option[Explanation],
errorGroupPath: ErrorGroupPath,
errorGroupPath: ErrorClass,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package com.daml.error.generator
import com.daml.error.utils.testpackage.subpackage.MildErrors
import com.daml.error.utils.testpackage.subpackage.MildErrors.NotSoSeriousError
import com.daml.error.utils.testpackage.{DeprecatedError, SeriousError}
import com.daml.error.{Deprecation, Explanation, ErrorGroupSegment, Resolution}
import com.daml.error.{Deprecation, Explanation, Grouping, Resolution}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

package com.daml.error

/** A component of [[ErrorGroupPath]]
/** A component of [[ErrorClass]]
*
* @param docName The name that will appear in the generated documentation for the grouping.
* @param fullClassName Full class name of the corresponding [[ErrorGroup]].
*/
case class ErrorGroupSegment(
case class Grouping(
docName: String,
fullClassName: String,
) {
Expand All @@ -20,11 +20,11 @@ case class ErrorGroupSegment(

/** Used to hierarchically structure error codes in the official documentation.
*/
case class ErrorGroupPath(segments: List[ErrorGroupSegment]) {
def extend(last: ErrorGroupSegment): ErrorGroupPath =
ErrorGroupPath(segments :+ last)
case class ErrorClass(segments: List[Grouping]) {
def extend(last: Grouping): ErrorClass =
ErrorClass(segments :+ last)
}

object ErrorGroupPath {
def root(): ErrorGroupPath = ErrorGroupPath(Nil)
object ErrorClass {
def root(): ErrorClass = ErrorClass(Nil)
}
2 changes: 1 addition & 1 deletion ledger/error/src/main/scala/com/daml/error/ErrorCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import scala.util.matching.Regex
* }
*/
abstract class ErrorCode(val id: String, val category: ErrorCategory)(implicit
val errorGroupPath: ErrorGroupPath
val errorGroupPath: ErrorClass
) {

require(id.nonEmpty, "error-id must be non empty")
Expand Down
8 changes: 4 additions & 4 deletions ledger/error/src/main/scala/com/daml/error/ErrorGroup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

package com.daml.error

abstract class ErrorGroup()(implicit parent: ErrorGroupPath) {
abstract class ErrorGroup()(implicit parent: ErrorClass) {
val fullClassName: String = getClass.getName
// Hit https://github.com/scala/bug/issues/5425?orig=1 here: we cannot use .getSimpleName in deeply nested objects
// TODO error codes: Switch to using .getSimpleName when switching to JDK 9+
implicit val errorGroupPath: ErrorGroupPath = resolveErrorClass()
implicit val errorGroupPath: ErrorClass = resolveErrorClass()

private def resolveErrorClass(): ErrorGroupPath = {
private def resolveErrorClass(): ErrorClass = {
val name = fullClassName
.replace("$", ".")
.split("\\.")
Expand All @@ -21,6 +21,6 @@ abstract class ErrorGroup()(implicit parent: ErrorGroupPath) {
s"Could not parse full class name: '${fullClassName}' for the error class name"
)
)
parent.extend(ErrorGroupSegment(docName = name, fullClassName = fullClassName))
parent.extend(Grouping(docName = name, fullClassName = fullClassName))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

package com.daml.error.definitions

import com.daml.error.{ErrorGroupPath, ErrorGroup}
import com.daml.error.{ErrorClass, ErrorGroup}

object ErrorGroups {
val rootErrorClass: ErrorGroupPath = ErrorGroupPath.root()
val rootErrorClass: ErrorClass = ErrorClass.root()

object ParticipantErrorGroup extends ErrorGroup()(rootErrorClass) {
abstract class IndexErrorGroup extends ErrorGroup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ import org.scalatest.matchers.should.Matchers

class ErrorGroupSpec extends AnyFlatSpec with Matchers with BeforeAndAfter {

object ErrorGroupBar extends ErrorGroup()(ErrorGroupPath.root())
object ErrorGroupBar extends ErrorGroup()(ErrorClass.root())

object ErrorGroupFoo1 extends ErrorGroup()(ErrorGroupPath.root()) {
object ErrorGroupFoo1 extends ErrorGroup()(ErrorClass.root()) {
object ErrorGroupFoo2 extends ErrorGroup() {
object ErrorGroupFoo3 extends ErrorGroup()
}
}

it should "resolve correct error group names" in {
ErrorGroupFoo1.ErrorGroupFoo2.ErrorGroupFoo3.errorGroupPath shouldBe ErrorGroupPath(
ErrorGroupFoo1.ErrorGroupFoo2.ErrorGroupFoo3.errorGroupPath shouldBe ErrorClass(
List(
ErrorGroupSegment("ErrorGroupFoo1", ErrorGroupFoo1.fullClassName),
ErrorGroupSegment("ErrorGroupFoo2", ErrorGroupFoo1.ErrorGroupFoo2.fullClassName),
ErrorGroupSegment(
Grouping("ErrorGroupFoo1", ErrorGroupFoo1.fullClassName),
Grouping("ErrorGroupFoo2", ErrorGroupFoo1.ErrorGroupFoo2.fullClassName),
Grouping(
"ErrorGroupFoo3",
ErrorGroupFoo1.ErrorGroupFoo2.ErrorGroupFoo3.fullClassName,
),
)
)
ErrorGroupBar.errorGroupPath shouldBe ErrorGroupPath(
List(ErrorGroupSegment("ErrorGroupBar", ErrorGroupBar.fullClassName))
ErrorGroupBar.errorGroupPath shouldBe ErrorClass(
List(Grouping("ErrorGroupBar", ErrorGroupBar.fullClassName))
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
package com.daml.error.utils.testpackage

import com.daml.error.{Explanation, Resolution}
import com.daml.error.{BaseError, ErrorCategory, ErrorGroupPath, ErrorCode}
import com.daml.error.{BaseError, ErrorCategory, ErrorClass, ErrorCode}
import com.daml.logging.LoggingContext

@deprecated("deprecated.")
@Resolution("Turn it off and on again.")
@Explanation("Things happen.")
case object DeprecatedError
extends ErrorCode("DEPRECATED_ERROR", ErrorCategory.SystemInternalAssumptionViolated)(
ErrorGroupPath.root()
ErrorClass.root()
) {
case class Error(cause: String)(implicit val loggingContext: LoggingContext) extends BaseError {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
package com.daml.error.utils.testpackage

import com.daml.error.{Explanation, Resolution}
import com.daml.error.{BaseError, ErrorCategory, ErrorGroupPath, ErrorCode}
import com.daml.error.{BaseError, ErrorCategory, ErrorClass, ErrorCode}
import com.daml.logging.LoggingContext

@Explanation("Things happen.")
@Resolution("Turn it off and on again.")
case object SeriousError
extends ErrorCode("BLUE_SCREEN", ErrorCategory.SystemInternalAssumptionViolated)(
ErrorGroupPath.root()
ErrorClass.root()
) {
case class Error(cause: String)(implicit val loggingContext: LoggingContext) extends BaseError {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import com.daml.logging.LoggingContext
@Explanation("Groups mild errors together")
object MildErrors
extends ErrorGroup()(
parent =
ErrorGroupPath.root().extend(ErrorGroupSegment("Some grouping", "full.class.Name123"))
parent = ErrorClass.root().extend(Grouping("Some grouping", "full.class.Name123"))
) {

@Explanation("Test: Things like this always happen.")
Expand Down