Index: /sandbox/webservice/api/rest/dispatch.php
===================================================================
--- /sandbox/webservice/api/rest/dispatch.php	(revision 6025)
+++ /sandbox/webservice/api/rest/dispatch.php	(revision 6061)
@@ -51,8 +51,5 @@
 			break;
 		default:
-			$expresso = new Expresso($request->id);
-			$expresso->post($request);
-			$expresso->setError("".$e->getCode(), $request->uri);			
-			$response = $expresso->getResponse();			
+			$response = Errors::getResponse($request, $e);
 	}
 }
Index: /sandbox/webservice/api/rest/core/Errors.php
===================================================================
--- /sandbox/webservice/api/rest/core/Errors.php	(revision 6019)
+++ /sandbox/webservice/api/rest/core/Errors.php	(revision 6061)
@@ -58,16 +58,33 @@
 	);
 		
-	static public function get($code, $arg){				
-		$errors = self::$reservedErrors + self::$applicationErrors;
+	static public function set($code, $message){
+		return array ("code" => $code, "message" => $message);
+	}
+
+	static public function get($code, $argument = false){
+		$errors = self::$reservedErrors + self::$applicationErrors;		
 		$message = $errors[$code];
-		if($arg){
-			$message = str_replace("%1", $arg, $message);
-		}
-		if(!$message){
+		if(!$message)
 			$message = $errors[self::E_UNKNOWN_ERROR];
-		}
+		if($argument)
+			$message = str_replace("%1", $argument, $message);		
 		
-		return array ("code" => $code, "message" => $message);				
-	}			
+		return array ("code" => $code, "message" => $message); 
+	}
+	
+	static public function getResponse($request, $responseException){			
+			$response = new Response($request);
+			$response->code = Response::OK;
+			$response->addHeader('content-type', 'application/json');
+			if($request->id)
+				$body['id']	= $request->id;
+			
+			$code	 = $responseException->getCode();
+			$error = Errors::get($code);			
+			$message = ($responseException->getMessage() ? $responseException->getMessage() : $error['message']);			
+			$body['error'] = array("code" => $code, "message" => $message);		
+			$response->body = json_encode($body);
+			return $response;
+	}
 		
 }
Index: /sandbox/webservice/api/rest/core/LoginResource.php
===================================================================
--- /sandbox/webservice/api/rest/core/LoginResource.php	(revision 6019)
+++ /sandbox/webservice/api/rest/core/LoginResource.php	(revision 6061)
@@ -4,30 +4,18 @@
 	public function post($request){
 		// to Receive POST Params (use $this->params)
- 		parent::post($request);	
-		
-		if(!$this-> isLoggedIn()) 
-		{					
-			if($sessionid = $GLOBALS['phpgw']->session->create($this->getParam('user'), $this->getParam('password')))					
-			{				
-				$result = array(
-					'auth' 			=> $sessionid.":".$GLOBALS['phpgw']->session->kp3,
-					'profile' 		=> array($this->getUserProfile())
-				);
-				$this->setError(false);
-				$this->setResult($result);
-			}
-			else
-			{	
-				$this->setError($GLOBALS['phpgw']->session->cd_reason);
-			}			
+ 		parent::post($request);
+		if($sessionid = $GLOBALS['phpgw']->session->create($this->getParam('user'), $this->getParam('password')))
+		{
+			$result = array(
+				'auth' 			=> $sessionid.":".$GLOBALS['phpgw']->session->kp3,
+				'profile' 		=> array($this->getUserProfile())
+			);
+			$this->setResult($result);
 		}
 		else
 		{
-			$result = array('auth' =>  $this->getParam('auth'));
-			$this->setResult($result);
+			throw new ResponseException("",$GLOBALS['phpgw']->session->cd_reason);
 		}
-		
-		//to Send Response (JSON RPC format)
-		return $this->getResponse();		
+		return $this->getResponse();
 	}	
 
Index: /sandbox/webservice/api/rest/core/Expresso.php
===================================================================
--- /sandbox/webservice/api/rest/core/Expresso.php	(revision 6022)
+++ /sandbox/webservice/api/rest/core/Expresso.php	(revision 6061)
@@ -59,9 +59,6 @@
 	}
 	
-	public function setError($errorID, $arg1 = false) {
-		if($errorID)
-			$this-> error = Errors::get($errorID, $arg1);
-		else
-			$this-> error = null;
+	public function setError($error) {
+		$this-> error = $error;
 	}
 	
@@ -96,20 +93,11 @@
 		$response->addHeader('content-type', 'application/json');
 
+		if($this->getId())
+			$body['id']	= $this->getId();
+		if($this->getResult())
+			$body['result']	= $this->getResult();
+		else
+			throw new ResponseException("",Errors::E_UNKNOWN_ERROR);
 		
-		if($this->getResult()) {
-			$body['result']	= $this->getResult();
-		}
-		elseif($this->getError()){
-			$body['error'] = $this->getError();
-		}
-		else{
-			$this->setError(Errors::E_INTERNAL_ERROR);
-			$body['error'] = $this->getError();
-		}
-		
-		if($this->getId()) {
-			$body['id']	= $this->getId();
-		}
-
 		$response->body = json_encode($body);
 		
@@ -143,14 +131,12 @@
 			list($sessionid, $kp3) = explode(":", $this->getParam('auth'));
 			if($GLOBALS['phpgw']->session->verify($sessionid, $kp3)){									
-				return true;
+				return $sessionid;
 			}
 			else{
-				$this-> setError(Errors::LOGIN_AUTH_INVALID);
-				return false;
+				throw new ResponseException("",Errors::LOGIN_AUTH_INVALID);
 			}
 		}
 		else{
-			$this-> setError(Errors::LOGIN_NOT_LOGGED_IN);
-			return false;
+			throw new ResponseException("",Errors::LOGIN_NOT_LOGGED_IN);
 		}		
 	}	
Index: /sandbox/webservice/api/rest/catalog/ContactsResource.php
===================================================================
--- /sandbox/webservice/api/rest/catalog/ContactsResource.php	(revision 6019)
+++ /sandbox/webservice/api/rest/catalog/ContactsResource.php	(revision 6061)
@@ -24,6 +24,6 @@
 						return $this->getGlobalContacts($search, $this->getParam('contactID'));
 					else{
-						$this->setError(Errors::CATALOG_MIN_ARGUMENT_SEARCH, $this-> getMinArgumentSearch());						
-						return $this->getResponse();
+						$error = Errors::get(Errors::CATALOG_MIN_ARGUMENT_SEARCH, $this-> getMinArgumentSearch());						
+						throw new ResponseException($error['message'], $error['code']);
 					}
 				}
