From ac4cd4afdfbf429af57e352213be3147ccfce94c Mon Sep 17 00:00:00 2001 From: Damien Levin Date: Wed, 19 Jun 2013 15:45:23 -0400 Subject: [PATCH] Added BrandoException --- README.md | 6 ++++++ src/main/scala/Brando.scala | 3 +++ src/main/scala/ReplyParser.scala | 2 +- src/test/scala/BrandoTest.scala | 2 ++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5e2f079..8b37e34 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,12 @@ Status replies are returned as case objects, such as `Pong` and `Ok`. // Response: Some(Ok) +Error replies are returned as `Failure` + + brando ! Request("EXPIRE", "1", "key") + + // Response: Failure(brando.BrandoException: ERR value is not an integer or out of range) + Integer replies are returned as `Option[Int]`. brando ! Request("SADD", "some-set", "one", "two") diff --git a/src/main/scala/Brando.scala b/src/main/scala/Brando.scala index 6c51180..96fe4fb 100644 --- a/src/main/scala/Brando.scala +++ b/src/main/scala/Brando.scala @@ -14,6 +14,9 @@ import annotation.tailrec import ExecutionContext.Implicits.global +private case class BrandoException(message: String) extends Exception(message) { + override lazy val toString = "%s: %s\n".format(getClass.getName, message) +} private case class Connect(address: InetSocketAddress) private case class CommandAck(sender: ActorRef) extends Tcp.Event private object StartProcess diff --git a/src/main/scala/ReplyParser.scala b/src/main/scala/ReplyParser.scala index c44c14a..d1050e4 100644 --- a/src/main/scala/ReplyParser.scala +++ b/src/main/scala/ReplyParser.scala @@ -59,7 +59,7 @@ private[brando] trait ReplyParser { buffer.take(length) match { case ErrorReply(reply) ⇒ val remainder = buffer.drop(length) - Success(Some(Status.Failure(new Exception(reply.utf8String))), remainder) + Success(Some(Status.Failure(new BrandoException(reply.utf8String))), remainder) case x ⇒ Failure(buffer) } diff --git a/src/test/scala/BrandoTest.scala b/src/test/scala/BrandoTest.scala index ea6f31b..063739c 100644 --- a/src/test/scala/BrandoTest.scala +++ b/src/test/scala/BrandoTest.scala @@ -221,6 +221,7 @@ class BrandoTest extends TestKit(ActorSystem("BrandoTest")) with FunSpec expectMsgPF(5.seconds) { case Status.Failure(e) ⇒ + assert(e.isInstanceOf[BrandoException]) assert(e.getMessage === "ERR wrong number of arguments for 'set' command") } @@ -228,6 +229,7 @@ class BrandoTest extends TestKit(ActorSystem("BrandoTest")) with FunSpec expectMsgPF(5.seconds) { case Status.Failure(e) ⇒ + assert(e.isInstanceOf[BrandoException]) assert(e.getMessage === "ERR value is not an integer or out of range") } }