|
| 1 | +/* |
| 2 | + * Copyright © 2015 - 2019 Lightbend, Inc. <http://www.lightbend.com> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.lightbend.paradox |
| 18 | + |
| 19 | +import java.io.File |
| 20 | + |
| 21 | +import com.lightbend.paradox.markdown.Page |
| 22 | +import org.pegdown.ast.Node |
| 23 | + |
| 24 | +import scala.collection.mutable.ListBuffer |
| 25 | + |
| 26 | +trait ErrorContext { |
| 27 | + def apply(msg: String, index: Int): Unit |
| 28 | + |
| 29 | + def apply(msg: String, node: Node): Unit = apply(msg, node.getStartIndex) |
| 30 | + |
| 31 | + def apply(msg: String): Unit |
| 32 | + |
| 33 | + def apply(msg: String, file: File, index: Int): Unit |
| 34 | + |
| 35 | + def apply(msg: String, file: File, node: Node): Unit = apply(msg, file, node.getStartIndex) |
| 36 | + |
| 37 | + def apply(msg: String, file: File): Unit |
| 38 | + |
| 39 | + def apply(msg: String, page: Page, index: Int): Unit = apply(msg, page.file, index) |
| 40 | + |
| 41 | + def apply(msg: String, page: Page, node: Node): Unit = apply(msg, page.file, node.getStartIndex) |
| 42 | + |
| 43 | + def apply(msg: String, page: Page): Unit = apply(msg, page.file) |
| 44 | +} |
| 45 | + |
| 46 | +class ErrorCollector extends ErrorContext { |
| 47 | + private val errors = new ListBuffer[ParadoxError]() |
| 48 | + |
| 49 | + private def addError(msg: String, page: Option[File], index: Option[Int]) = { |
| 50 | + errors.append(ParadoxError(msg, page, index)) |
| 51 | + } |
| 52 | + |
| 53 | + override def apply(msg: String, index: Int): Unit = throw new IllegalArgumentException("Cannot report an indexed error without a page context") |
| 54 | + |
| 55 | + override def apply(msg: String): Unit = addError(msg, None, None) |
| 56 | + |
| 57 | + override def apply(msg: String, file: File, index: Int): Unit = addError(msg, Some(file), Some(index)) |
| 58 | + |
| 59 | + override def apply(msg: String, file: File): Unit = addError(msg, Some(file), None) |
| 60 | + |
| 61 | + def hasErrors: Boolean = errors.nonEmpty |
| 62 | + |
| 63 | + def errorCount: Int = errors.toList.distinct.size |
| 64 | + |
| 65 | + def logErrors(log: ParadoxLogger) = { |
| 66 | + val totalErrors = errors.toList.distinct |
| 67 | + // First log general errors |
| 68 | + totalErrors.foreach { |
| 69 | + case ParadoxError(msg, None, _) => log.error(msg) |
| 70 | + case _ => |
| 71 | + } |
| 72 | + // Now handle page specific errors |
| 73 | + totalErrors |
| 74 | + .filter(_.page.isDefined) |
| 75 | + .groupBy(_.page.get) |
| 76 | + .toSeq |
| 77 | + .sortBy(_._1.getAbsolutePath) |
| 78 | + .foreach { |
| 79 | + case (page, errors) => |
| 80 | + // Load contents of the page |
| 81 | + val lines = scala.io.Source.fromFile(page)("UTF-8").getLines().toList |
| 82 | + errors.sortBy(_.index.getOrElse(0)).foreach { |
| 83 | + case ParadoxError(error, _, Some(idx)) => |
| 84 | + val (_, lineNo, colNo, line) = lines.foldLeft((0, 0, 0, None: Option[String])) { (state, line) => |
| 85 | + state match { |
| 86 | + case (_, _, _, Some(_)) => state |
| 87 | + case (total, l, c, None) => |
| 88 | + if (total + line.length < idx) { |
| 89 | + (total + line.length + 1, l + 1, c, None) |
| 90 | + } else { |
| 91 | + (0, l + 1, idx - total + 1, Some(line)) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + log.error(s"$error at ${page.getAbsolutePath}:$lineNo") |
| 97 | + line.foreach { l => |
| 98 | + log.error(l) |
| 99 | + log.error(l.take(colNo - 1).map { case '\t' => '\t'; case _ => ' ' } + "^") |
| 100 | + } |
| 101 | + case ParadoxError(error, _, _) => |
| 102 | + log.error(s"$error at ${page.getAbsolutePath}") |
| 103 | + |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +class PagedErrorContext(context: ErrorContext, page: Page) extends ErrorContext { |
| 110 | + override def apply(msg: String, index: Int): Unit = context.apply(msg, page, index) |
| 111 | + |
| 112 | + override def apply(msg: String): Unit = context.apply(msg, page) |
| 113 | + |
| 114 | + override def apply(msg: String, file: File, index: Int): Unit = context.apply(msg, file, index) |
| 115 | + |
| 116 | + override def apply(msg: String, file: File): Unit = context.apply(msg, file) |
| 117 | +} |
| 118 | + |
| 119 | +class ThrowingErrorContext extends ErrorContext { |
| 120 | + override def apply(msg: String, index: Int): Unit = throw ParadoxException(ParadoxError(msg, None, Some(index))) |
| 121 | + |
| 122 | + override def apply(msg: String): Unit = throw ParadoxException(ParadoxError(msg, None, None)) |
| 123 | + |
| 124 | + override def apply(msg: String, file: File, index: Int): Unit = throw ParadoxException(ParadoxError(msg, Some(file), Some(index))) |
| 125 | + |
| 126 | + override def apply(msg: String, file: File): Unit = throw ParadoxException(ParadoxError(msg, Some(file), None)) |
| 127 | +} |
| 128 | + |
| 129 | +case class ParadoxError(msg: String, page: Option[File], index: Option[Int]) |
| 130 | + |
| 131 | +case class ParadoxException(error: ParadoxError) extends RuntimeException(error.msg) |
0 commit comments