Ignore:
Timestamp:
04/27/12 15:18:00 (12 years ago)
Author:
niltonneto
Message:

Ticket #2507 - Modificada implementação para tratamento de erros por Exception.

Location:
sandbox/webservice/api/rest
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • sandbox/webservice/api/rest/catalog/ContactsResource.php

    r6019 r6061  
    2424                                                return $this->getGlobalContacts($search, $this->getParam('contactID')); 
    2525                                        else{ 
    26                                                 $this->setError(Errors::CATALOG_MIN_ARGUMENT_SEARCH, $this-> getMinArgumentSearch());                                            
    27                                                 return $this->getResponse(); 
     26                                                $error = Errors::get(Errors::CATALOG_MIN_ARGUMENT_SEARCH, $this-> getMinArgumentSearch());                                               
     27                                                throw new ResponseException($error['message'], $error['code']); 
    2828                                        } 
    2929                                } 
  • sandbox/webservice/api/rest/core/Errors.php

    r6019 r6061  
    5858        ); 
    5959                 
    60         static public function get($code, $arg){                                 
    61                 $errors = self::$reservedErrors + self::$applicationErrors; 
     60        static public function set($code, $message){ 
     61                return array ("code" => $code, "message" => $message); 
     62        } 
     63 
     64        static public function get($code, $argument = false){ 
     65                $errors = self::$reservedErrors + self::$applicationErrors;              
    6266                $message = $errors[$code]; 
    63                 if($arg){ 
    64                         $message = str_replace("%1", $arg, $message); 
    65                 } 
    66                 if(!$message){ 
     67                if(!$message) 
    6768                        $message = $errors[self::E_UNKNOWN_ERROR]; 
    68                 } 
     69                if($argument) 
     70                        $message = str_replace("%1", $argument, $message);               
    6971                 
    70                 return array ("code" => $code, "message" => $message);                           
    71         }                        
     72                return array ("code" => $code, "message" => $message);  
     73        } 
     74         
     75        static public function getResponse($request, $responseException){                        
     76                        $response = new Response($request); 
     77                        $response->code = Response::OK; 
     78                        $response->addHeader('content-type', 'application/json'); 
     79                        if($request->id) 
     80                                $body['id']     = $request->id; 
     81                         
     82                        $code    = $responseException->getCode(); 
     83                        $error = Errors::get($code);                     
     84                        $message = ($responseException->getMessage() ? $responseException->getMessage() : $error['message']);                    
     85                        $body['error'] = array("code" => $code, "message" => $message);          
     86                        $response->body = json_encode($body); 
     87                        return $response; 
     88        } 
    7289                 
    7390} 
  • sandbox/webservice/api/rest/core/Expresso.php

    r6022 r6061  
    5959        } 
    6060         
    61         public function setError($errorID, $arg1 = false) { 
    62                 if($errorID) 
    63                         $this-> error = Errors::get($errorID, $arg1); 
    64                 else 
    65                         $this-> error = null; 
     61        public function setError($error) { 
     62                $this-> error = $error; 
    6663        } 
    6764         
     
    9693                $response->addHeader('content-type', 'application/json'); 
    9794 
     95                if($this->getId()) 
     96                        $body['id']     = $this->getId(); 
     97                if($this->getResult()) 
     98                        $body['result'] = $this->getResult(); 
     99                else 
     100                        throw new ResponseException("",Errors::E_UNKNOWN_ERROR); 
    98101                 
    99                 if($this->getResult()) { 
    100                         $body['result'] = $this->getResult(); 
    101                 } 
    102                 elseif($this->getError()){ 
    103                         $body['error'] = $this->getError(); 
    104                 } 
    105                 else{ 
    106                         $this->setError(Errors::E_INTERNAL_ERROR); 
    107                         $body['error'] = $this->getError(); 
    108                 } 
    109                  
    110                 if($this->getId()) { 
    111                         $body['id']     = $this->getId(); 
    112                 } 
    113  
    114102                $response->body = json_encode($body); 
    115103                 
     
    143131                        list($sessionid, $kp3) = explode(":", $this->getParam('auth')); 
    144132                        if($GLOBALS['phpgw']->session->verify($sessionid, $kp3)){                                                                        
    145                                 return true; 
     133                                return $sessionid; 
    146134                        } 
    147135                        else{ 
    148                                 $this-> setError(Errors::LOGIN_AUTH_INVALID); 
    149                                 return false; 
     136                                throw new ResponseException("",Errors::LOGIN_AUTH_INVALID); 
    150137                        } 
    151138                } 
    152139                else{ 
    153                         $this-> setError(Errors::LOGIN_NOT_LOGGED_IN); 
    154                         return false; 
     140                        throw new ResponseException("",Errors::LOGIN_NOT_LOGGED_IN); 
    155141                }                
    156142        }        
  • sandbox/webservice/api/rest/core/LoginResource.php

    r6019 r6061  
    44        public function post($request){ 
    55                // to Receive POST Params (use $this->params) 
    6                 parent::post($request);  
    7                  
    8                 if(!$this-> isLoggedIn())  
    9                 {                                        
    10                         if($sessionid = $GLOBALS['phpgw']->session->create($this->getParam('user'), $this->getParam('password')))                                        
    11                         {                                
    12                                 $result = array( 
    13                                         'auth'                  => $sessionid.":".$GLOBALS['phpgw']->session->kp3, 
    14                                         'profile'               => array($this->getUserProfile()) 
    15                                 ); 
    16                                 $this->setError(false); 
    17                                 $this->setResult($result); 
    18                         } 
    19                         else 
    20                         {        
    21                                 $this->setError($GLOBALS['phpgw']->session->cd_reason); 
    22                         }                        
     6                parent::post($request); 
     7                if($sessionid = $GLOBALS['phpgw']->session->create($this->getParam('user'), $this->getParam('password'))) 
     8                { 
     9                        $result = array( 
     10                                'auth'                  => $sessionid.":".$GLOBALS['phpgw']->session->kp3, 
     11                                'profile'               => array($this->getUserProfile()) 
     12                        ); 
     13                        $this->setResult($result); 
    2314                } 
    2415                else 
    2516                { 
    26                         $result = array('auth' =>  $this->getParam('auth')); 
    27                         $this->setResult($result); 
     17                        throw new ResponseException("",$GLOBALS['phpgw']->session->cd_reason); 
    2818                } 
    29                  
    30                 //to Send Response (JSON RPC format) 
    31                 return $this->getResponse();             
     19                return $this->getResponse(); 
    3220        }        
    3321 
  • sandbox/webservice/api/rest/dispatch.php

    r6025 r6061  
    5151                        break; 
    5252                default: 
    53                         $expresso = new Expresso($request->id); 
    54                         $expresso->post($request); 
    55                         $expresso->setError("".$e->getCode(), $request->uri);                    
    56                         $response = $expresso->getResponse();                    
     53                        $response = Errors::getResponse($request, $e); 
    5754        } 
    5855} 
Note: See TracChangeset for help on using the changeset viewer.