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 + } + +}