Skip to content

Commit

Permalink
TwinagleException includes error code in message (#388)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bscherlein and ccmtaylor authored Sep 29, 2023
1 parent 240ee26 commit 32e82ce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -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
}

}

0 comments on commit 32e82ce

Please sign in to comment.