Skip to content

Commit f359a33

Browse files
author
Ivan Luzyanin
committed
Print message formatting problems to debug console to avoid exceptions like j.u.UnknownFormatConversionException
1 parent c9dae13 commit f359a33

File tree

5 files changed

+78
-13
lines changed

5 files changed

+78
-13
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ RUN addgroup -g 9000 -S code && \
2424
adduser -S -G code app
2525
USER app
2626

27-
COPY --from=builder /code/target/scala-2.12/codeclimate-scalastyle-assembly-0.1.0.jar /usr/src/app/engine-core.jar
27+
COPY --from=builder /code/target/scala-2.12/codeclimate-scalastyle-assembly-*.jar /usr/src/app/engine-core.jar
2828
COPY src/main/resources/docker /usr/src/app
2929
COPY src/main/resources/docker/engine.json /
3030

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name := "codeclimate-scalastyle"
22
organization in ThisBuild := "codeclimate"
3-
version in ThisBuild := "0.1.0"
3+
version in ThisBuild := "0.1.1"
44
scalaVersion in ThisBuild := "2.12.2"
55

66
concurrentRestrictions in Global += Tags.limit(Tags.Test, 1)

engine-core/src/main/scala/org/codeclimate/scalastyle/CodeClimateIssuePrinter.scala

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import io.circe.syntax._
99
import org.scalastyle.{FileSpec, Message, MessageHelper, StyleError}
1010

1111
import scala.collection.JavaConverters._
12+
import scala.util.{Failure, Success, Try}
1213

13-
class CodeClimateIssuePrinter(workspacePath: String, ps: PrintStream) {
14+
class CodeClimateIssuePrinter(workspacePath: String, ps: PrintStream, errPrinter: PrintStream = Console.err) {
1415
private val basePath = new File(workspacePath).toPath
1516
private val printer = Printer.noSpaces.copy(dropNullKeys = true)
1617

@@ -30,15 +31,24 @@ class CodeClimateIssuePrinter(workspacePath: String, ps: PrintStream) {
3031
val msg: String = se.customMessage.orElse {
3132
Some(messageHelper.message(se.key, se.args))
3233
}.getOrElse("Error message not provided")
33-
val issue = Issue(location = location,
34-
description = String.format(msg, se.args.asJava),
35-
check_name = Some(se.clazz.getName),
36-
categories = Seq("Style"),
37-
severity = Some("major")
38-
)
39-
val jsonStr = printer.pretty(issue.asJson)
40-
ps.print(jsonStr)
41-
ps.print("\0")
34+
Try(String.format(msg, se.args.asJava)) match {
35+
case Success(formattedMessage) =>
36+
val issue = Issue(location = location,
37+
description = formattedMessage,
38+
check_name = Some(se.clazz.getName),
39+
categories = Seq("Style"),
40+
severity = Some("major")
41+
)
42+
val jsonStr = printer.pretty(issue.asJson)
43+
ps.print(jsonStr)
44+
ps.print("\0")
45+
case Failure(exception) =>
46+
// scalastyle:off
47+
errPrinter.println(s"Unable to format the message. Invalid message format for key=${se.key}: '$msg';" +
48+
s" args = '${se.args}'. Error: ${exception.printStackTrace()}"
49+
)
50+
// scalastyle:on
51+
}
4252
case _ => // ignore
4353
}
4454
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.codeclimate.scalastyle
2+
3+
import java.io.{BufferedOutputStream, PrintStream}
4+
5+
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream
6+
import org.scalastyle.file.FileTabChecker
7+
import org.scalastyle.{RealFileSpec, StyleError, _}
8+
import org.scalatest.{FunSuite, Matchers}
9+
10+
class CodeClimateIssuePrinterTest extends FunSuite with Matchers {
11+
12+
private val fileSpec = new RealFileSpec("src/main/scala/org/codeclimate/scalastyle/CodeClimateIssuePrinter.scala", None)
13+
14+
test("testPrintIssue") {
15+
val out = new ByteOutputStream()
16+
val err = new ByteOutputStream()
17+
val outPrinter = toPrintStream(out)
18+
val errPrinter = toPrintStream(err)
19+
val printer = new CodeClimateIssuePrinter(".", outPrinter, errPrinter)
20+
21+
printer.printIssue(new StyleError[RealFileSpec](fileSpec, classOf[FileTabChecker],
22+
new FileTabChecker().errorKey, ErrorLevel, List.empty[String], Some(0)))
23+
24+
outPrinter.flush()
25+
errPrinter.flush()
26+
27+
out.size() should not be 0
28+
err.size() should be(0)
29+
}
30+
31+
test("testPrintIssue bad format") {
32+
val out = new ByteOutputStream()
33+
val err = new ByteOutputStream()
34+
val outPrinter = toPrintStream(out)
35+
val errPrinter = toPrintStream(err)
36+
val printer = new CodeClimateIssuePrinter(".", outPrinter, errPrinter)
37+
38+
val errKey = new FileTabChecker().errorKey
39+
printer.printIssue(new StyleError[RealFileSpec](fileSpec, classOf[FileTabChecker],
40+
errKey, ErrorLevel, List.empty[String], Some(0), customMessage = Some(
41+
"This is badly formatted custom message %'*'"
42+
)))
43+
44+
outPrinter.flush()
45+
errPrinter.flush()
46+
47+
out.size() should be(0)
48+
err.size() should not be 0
49+
err.toString should include(s"Invalid message format for key=$errKey")
50+
}
51+
52+
private def toPrintStream(out: ByteOutputStream): PrintStream = {
53+
new PrintStream(new BufferedOutputStream(out))
54+
}
55+
}

src/main/resources/docker/engine.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"email": "jsippel@acorns.com"
77
},
88
"languages" : ["Scala"],
9-
"version": "0.1.0",
9+
"version": "0.1.1",
1010
"spec_version": "0.3.1"
1111
}

0 commit comments

Comments
 (0)