-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #531 from typelevel/api/adt-companions
preparing repackaging for 1.0 - move ADT members to companion + some more removals from public API
- Loading branch information
Showing
105 changed files
with
653 additions
and
581 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
core/shared/src/main/scala/laika/api/errors/errors.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package laika.api.errors | ||
|
||
import cats.syntax.all.* | ||
import cats.data.{ Chain, NonEmptyChain } | ||
import laika.ast.{ Document, DocumentTreeRoot, Invalid, MessageFilter, Path } | ||
import laika.config.ConfigError | ||
import laika.config.ConfigError.TreeConfigErrors | ||
|
||
sealed trait TransformationError { | ||
def message: String | ||
} | ||
|
||
case class RendererError(message: String) extends TransformationError { | ||
override def toString: String = message | ||
} | ||
|
||
object RendererError { | ||
|
||
def apply(message: String): RendererError = | ||
new RendererError(s"Rendering Error: $message") | ||
|
||
def apply(configError: ConfigError): RendererError = | ||
RendererError(s"Configuration Error: ${configError.message}") | ||
|
||
} | ||
|
||
case class ParserError(message: String) extends TransformationError { | ||
override def toString: String = message | ||
} | ||
|
||
object ParserError { | ||
|
||
def apply(message: String): ParserError = | ||
new ParserError(s"Error parsing input: $message") | ||
|
||
def apply(configError: ConfigError): ParserError = | ||
ParserError(s"Configuration Error: ${configError.message}") | ||
|
||
} | ||
|
||
private[laika] case class InvalidDocument( | ||
errors: Either[NonEmptyChain[ConfigError], NonEmptyChain[Invalid]], | ||
path: Path | ||
) extends RuntimeException( | ||
s"One or more errors processing document '$path': ${InvalidDocument.format(errors, path)}" | ||
) | ||
|
||
private[laika] object InvalidDocument { | ||
|
||
def apply(path: Path, error: ConfigError, errors: ConfigError*): InvalidDocument = | ||
new InvalidDocument(Left(NonEmptyChain.fromChainPrepend(error, Chain.fromSeq(errors))), path) | ||
|
||
def apply(path: Path, error: Invalid, errors: Invalid*): InvalidDocument = | ||
new InvalidDocument(Right(NonEmptyChain.fromChainPrepend(error, Chain.fromSeq(errors))), path) | ||
|
||
def indent(lineContent: String): String = { | ||
val lines = lineContent.split('\n') | ||
lines.head + "\n " + lines.last | ||
} | ||
|
||
def format( | ||
errors: Either[NonEmptyChain[ConfigError], NonEmptyChain[Invalid]], | ||
path: Path | ||
): String = | ||
errors.fold( | ||
configErrors => configErrors.map(_.message).mkString_("\n"), | ||
invalidElems => invalidElems.map(InvalidDocument.formatElement(path)).toList.mkString | ||
) | ||
|
||
def format(doc: InvalidDocument): String = format(doc.errors, doc.path) | ||
|
||
def formatElement(docPath: Path)(element: Invalid): String = { | ||
val pathStr = element.source.path.fold("") { srcPath => | ||
if (srcPath == docPath) "" else srcPath.toString + ":" | ||
} | ||
s""" [$pathStr${element.source.position.line}]: ${element.message.content} | ||
| | ||
| ${indent(element.source.position.lineContentWithCaret)} | ||
| | ||
|""".stripMargin | ||
} | ||
|
||
def from(document: Document, failOn: MessageFilter): Option[InvalidDocument] = { | ||
val invalidElements = document.invalidElements(failOn) | ||
NonEmptyChain.fromSeq(invalidElements).map(inv => InvalidDocument(Right(inv), document.path)) | ||
} | ||
|
||
} | ||
|
||
private[laika] case class InvalidDocuments(documents: NonEmptyChain[InvalidDocument]) | ||
extends RuntimeException( | ||
s"One or more invalid documents:\n${InvalidDocuments.format(documents)}" | ||
) | ||
|
||
private[laika] object InvalidDocuments { | ||
|
||
def format(documents: NonEmptyChain[InvalidDocument]): String = { | ||
|
||
def formatDoc(doc: InvalidDocument): String = | ||
s"""${doc.path} | ||
| | ||
|${InvalidDocument.format(doc)}""".stripMargin | ||
|
||
documents.map(formatDoc).mkString_("").trim | ||
} | ||
|
||
def from( | ||
result: Either[TreeConfigErrors, DocumentTreeRoot], | ||
failOn: MessageFilter | ||
): Either[InvalidDocuments, DocumentTreeRoot] = { | ||
result.fold( | ||
errors => | ||
Left( | ||
InvalidDocuments( | ||
errors.failures.map(err => InvalidDocument(Left(err.failures), err.path)) | ||
) | ||
), | ||
root => from(root, failOn).toLeft(root) | ||
) | ||
} | ||
|
||
def from(root: DocumentTreeRoot, failOn: MessageFilter): Option[InvalidDocuments] = { | ||
val invalidDocs = root.allDocuments | ||
.flatMap(InvalidDocument.from(_, failOn)) | ||
NonEmptyChain.fromSeq(invalidDocs) | ||
.map(InvalidDocuments(_)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.