You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@esme.apache.org by vd...@apache.org on 2009/03/13 00:29:15 UTC

svn commit: r753054 - /incubator/esme/trunk/server/src/main/scala/org/apache/esme/api/TwitterAPI.scala

Author: vdichev
Date: Thu Mar 12 23:29:15 2009
New Revision: 753054

URL: http://svn.apache.org/viewvc?rev=753054&view=rev
Log:
ESME-34 improved error messages for Twitter API

Modified:
    incubator/esme/trunk/server/src/main/scala/org/apache/esme/api/TwitterAPI.scala

Modified: incubator/esme/trunk/server/src/main/scala/org/apache/esme/api/TwitterAPI.scala
URL: http://svn.apache.org/viewvc/incubator/esme/trunk/server/src/main/scala/org/apache/esme/api/TwitterAPI.scala?rev=753054&r1=753053&r2=753054&view=diff
==============================================================================
--- incubator/esme/trunk/server/src/main/scala/org/apache/esme/api/TwitterAPI.scala (original)
+++ incubator/esme/trunk/server/src/main/scala/org/apache/esme/api/TwitterAPI.scala Thu Mar 12 23:29:15 2009
@@ -194,7 +194,7 @@
   }
   
   def userTimeline(userName: String): Box[TwitterResponse] = {
-    User.findFromWeb(userName) map (userTimeline)
+    User.findFromWeb(userName).map(userTimeline) ?~ "User not found"
   }
   
   def userTimeline(): Box[TwitterResponse] = {
@@ -245,7 +245,7 @@
   }
   
   def friends(userName: String): Box[TwitterResponse] = {
-    User.findFromWeb(userName) map(friends)
+    User.findFromWeb(userName).map(friends) ?~ "User not found"
   }
   
   def friends(): Box[TwitterResponse] = {
@@ -257,7 +257,7 @@
   }
   
   def followers(userName: String): Box[TwitterResponse] = {
-    User.findFromWeb(userName) map(followers)
+    User.findFromWeb(userName).map(followers) ?~ "User not found"
   }
   
   def followers(): Box[TwitterResponse] = {
@@ -276,7 +276,7 @@
 
   def createFriendship(other: String): Box[TwitterResponse] = {
     for (user <- calcUser;
-         other <- User.findFromWeb(other))
+         other <- User.findFromWeb(other) ?~ "User not found")
     yield {
       if (user follow other)
         Right(Map("user" -> userData(other)))
@@ -287,7 +287,7 @@
   
   def destroyFriendship(other: String): Box[TwitterResponse] = {
     for (user <- calcUser;
-         other <- User.findFromWeb(other))
+         other <- User.findFromWeb(other) ?~ "User not found")
     yield {
       if (user unfollow other)
         Right(Map("user" -> userData(other)))
@@ -312,15 +312,29 @@
     }
   }
   
-  private def calcUser(): Box[User] = 
-    LiftRules.authentication match {
-      case basicAuth: HttpBasicAuthentication =>
-        for (req <- S.request;
-             cred <- basicAuth.credentials(req);
-             user <- User.findFromWeb(cred._1))
-        yield user
-    }
-
+  private def calcUser(): Box[User] = {
+    val userBox =
+      LiftRules.authentication match {
+        case basicAuth: HttpBasicAuthentication =>
+          for (req <- S.request;
+               cred <- basicAuth.credentials(req);
+               user <- User.findFromWeb(cred._1))
+          yield user
+      }
+    userBox ?~ "User authentication failed"
+  }
+  
+  protected def unbox(x: () => Box[TwitterResponse]) = {
+    Either.merge(
+      x() match {
+        case Full(res) => res
+        case Empty => 
+          Right(Map("response" -> Nil))
+        case failMsg: Failure => 
+          Right(Map("hash" -> Map("error" -> failMsg.messageChain)))
+      }
+    )
+  }
 }
 
 object TwitterXmlAPI extends TwitterAPI with XMLApiHelper {
@@ -343,7 +357,7 @@
   override def dispatch: LiftRules.DispatchPF = {
     // modify the returned function to one which converts the result to XML
     dispatchMethod.andThen(x =>
-      {() => Full(nodeSeqToResponse(toXml(Either.merge(x().get)))) }
+      {() => Full(nodeSeqToResponse(toXml(unbox(x)))) }
     )
   }
 
@@ -360,7 +374,7 @@
   override def dispatch: LiftRules.DispatchPF = {
     // modify the returned function to one which converts the result to JSON
     dispatchMethod.andThen(x =>
-      {() => Full(JsonResponse(jsonAttributes(Either.merge(x().get)))) }
+      {() => Full(JsonResponse(jsonAttributes(unbox(x)))) }
     )
   }