From 32e82ce3cebfcdeab2bd8502f5ae0f44f6880c82 Mon Sep 17 00:00:00 2001 From: Barbara Scherlein Date: Fri, 29 Sep 2023 11:31:35 +0200 Subject: [PATCH] TwinagleException includes error code in message (#388) the Specs2 `throwsA` matcher only takes into account the exception class and message, leading to false-positive tests that expected equality/pattern-matching semantics (on the error code specifically). Update the exception message to include the error code as well. Co-authored-by: Christopher Taylor --- .../twinagle/TwinagleException.scala | 6 +++++- .../twinagle/TwinagleExceptionSpec.scala | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 runtime/src/test/scala/com/soundcloud/twinagle/TwinagleExceptionSpec.scala diff --git a/runtime/src/main/scala/com/soundcloud/twinagle/TwinagleException.scala b/runtime/src/main/scala/com/soundcloud/twinagle/TwinagleException.scala index 7b8e1741..46bf61cd 100644 --- a/runtime/src/main/scala/com/soundcloud/twinagle/TwinagleException.scala +++ b/runtime/src/main/scala/com/soundcloud/twinagle/TwinagleException.scala @@ -79,12 +79,16 @@ object ErrorCode { } } +object TwinagleException { + private def mkMessage(code: ErrorCode, msg: String): String = s"$msg [${code.desc}]" +} + case class TwinagleException( code: ErrorCode, msg: String, meta: Map[String, String] = Map.empty, cause: Throwable = null -) extends RuntimeException(msg, cause) { +) extends RuntimeException(TwinagleException.mkMessage(code, msg), cause) { def this(cause: Throwable) = this(ErrorCode.Internal, cause.toString, Map.empty, cause) } diff --git a/runtime/src/test/scala/com/soundcloud/twinagle/TwinagleExceptionSpec.scala b/runtime/src/test/scala/com/soundcloud/twinagle/TwinagleExceptionSpec.scala new file mode 100644 index 00000000..2d663ce6 --- /dev/null +++ b/runtime/src/test/scala/com/soundcloud/twinagle/TwinagleExceptionSpec.scala @@ -0,0 +1,20 @@ +package com.soundcloud.twinagle + +import org.specs2.matcher.ResultMatchers.beFailing +import org.specs2.mutable.Specification + +class TwinagleExceptionSpec extends Specification { + + "TwinagleException when seen as a RuntimeException, message should contain the error code" in { + val twinagleException = TwinagleException(ErrorCode.Internal, "something went wrong") + twinagleException.getMessage ==== "something went wrong [internal]" + } + + "specs2 throwsA(someTwinagleException) fails if someTwinagleException has different error code" in { + val internalTwinagleException = TwinagleException(ErrorCode.Internal, "my error message") + val expectedException = TwinagleException(ErrorCode.NotFound, "my error message") + def myFunction(): Unit = throw internalTwinagleException + (myFunction() must throwA(expectedException)) must beFailing + } + +}