Ignore:
Timestamp:
05/11/12 10:38:02 (12 years ago)
Author:
niltonneto
Message:

Ticket #2507 - Refatorado e centralizado tratamento de Erros.

Location:
sandbox/webservice/api
Files:
3 added
9 edited
1 moved

Legend:

Unmodified
Added
Removed
  • sandbox/webservice/api/adapters/ExpressoAdapter.php

    r6149 r6165  
    100100                if($this->getResult()) 
    101101                        $body['result'] = $this->getResult(); 
    102                 else 
    103                         throw new ResponseException("",Errors::E_UNKNOWN_ERROR); 
     102                else { 
     103                        Errors::runException("E_UNKNOWN_ERROR");                         
     104                } 
     105                 
    104106                 
    105107                $response->body = json_encode($body); 
     
    115117                        } 
    116118                        else{ 
    117                                 throw new ResponseException("",Errors::LOGIN_AUTH_INVALID); 
     119                                Errors::runException("LOGIN_AUTH_INVALID");                                                      
    118120                        } 
    119121                } 
    120122                else{ 
    121                         throw new ResponseException("",Errors::LOGIN_NOT_LOGGED_IN); 
     123                        Errors::runException("LOGIN_NOT_LOGGED_IN");                     
    122124                }                
    123125        }        
  • sandbox/webservice/api/library/utils/Errors.php

    r6150 r6165  
    33final class Errors { 
    44                 
    5         // Errors from -32000 to -32099 => Server error :Reserved for implementation-defined server-errors. 
    6         const E_PARSE_ERROR             = "-32700";      
    7         const E_INVALID_REQUEST         = "-32600"; 
    8         const E_METHOD_NOT_FOUND        = "-32601"; 
    9         const E_INVALID_PARAMS          = "-32602"; 
    10         const E_INTERNAL_ERROR          = "-32603";      
    11  
    12         const E_HTTP_NOT_FOUND          = "404"; 
    13         const E_UNKNOWN_ERROR           = "500"; 
     5        private static $sInstance; 
     6        private $errors; 
    147         
    15          
    16         // Errors inherited from Expresso Application.   
    17         const LOGIN_SUCCESS_LOGOUT                      = "1"; 
    18         const LOGIN_SESSION_EXPIRED             = "2"; 
    19         const LOGIN_NOT_LOGGED_IN                       = "3"; 
    20         const LOGIN_COOKIES_REQUIRED            = "4"; 
    21         const LOGIN_BADLOGIN                            = "5"; 
    22         const LOGIN_PASSWORD_EXPIRED            = "6"; 
    23         const LOGIN_AUTH_INVALID                        = "7"; 
    24         const LOGIN_SESSION_NOT_VERIFIED        = "10"; 
    25         const LOGIN_ACCOUNT_EXPIRED             = "98"; 
    26         const LOGIN_LOGIN_BLOCKED                       = "99"; 
    27         const LOGIN_INVALID_LOGIN                       = "200"; 
    28          
    29  
    30         const CATALOG_MIN_ARGUMENT_SEARCH       = "1001";        
    31         const MAIL_NOT_SENT                                     = "1002"; 
    32         const MAIL_TRASH_NOT_CLEANED            = "1003"; 
    33         const MAIL_MESSAGE_NOT_FOUND            = "1004"; 
    34          
    35         static private $reservedErrors = array (                         
    36                 self::E_PARSE_ERROR             => "Parse error", 
    37                 self::E_INVALID_REQUEST         => "Invalid Request", 
    38                 self::E_METHOD_NOT_FOUND        => "Method not found", 
    39                 self::E_INVALID_PARAMS          => "Invalid params", 
    40                 self::E_INTERNAL_ERROR          => "Internal error", 
    41                 self::E_HTTP_NOT_FOUND          => 'A resource matching URI "%1" was not found', 
    42                 self::E_UNKNOWN_ERROR           => "HTTP Unknown error" 
    43         ); 
    44          
    45          
    46         // Errors inherited from Expresso Application. 
    47         static private $applicationErrors = array(                       
    48  
    49                 self::LOGIN_SUCCESS_LOGOUT                      => "You have been successfully logged out", 
    50                 self::LOGIN_SESSION_EXPIRED             => "Sorry, your login has expired", 
    51                 self::LOGIN_NOT_LOGGED_IN                       => "You are not logged in", 
    52                 self::LOGIN_COOKIES_REQUIRED            => "Cookies are required to login to this site.", 
    53                 self::LOGIN_BADLOGIN                            => "bad login or password", 
    54                 self::LOGIN_PASSWORD_EXPIRED            => "Your password has expired, and you do not have access to change it", 
    55                 self::LOGIN_AUTH_INVALID                        => "Your auth is invalid", 
    56                 self::LOGIN_SESSION_NOT_VERIFIED        => "Your session could not be verified.", 
    57                 self::LOGIN_ACCOUNT_EXPIRED             => "Account is expired", 
    58                 self::LOGIN_LOGIN_BLOCKED                       => "Blocked, too many attempts!", 
    59                 self::LOGIN_INVALID_LOGIN                       => "Bad login or password", 
    60                 self::CATALOG_MIN_ARGUMENT_SEARCH       => "Your search argument must be longer than %1 characters.", 
    61                 self::MAIL_NOT_SENT                                     => "Your mail could not be sent.", 
    62                 self::MAIL_TRASH_NOT_CLEANED            => "Your trash folder could not be cleaned.", 
    63                 self::MAIL_MESSAGE_NOT_FOUND            => "Message not found in folder %1." 
    64         ); 
    65                  
    66         static public function set($code, $message){ 
    67                 return array ("code" => $code, "message" => $message); 
    68         } 
    69  
    70         static public function get($code, $argument = false){ 
    71                 $errors = self::$reservedErrors + self::$applicationErrors;              
    72                 $message = $errors[$code]; 
    73                 if(!$message) 
    74                         $message = $errors[self::E_UNKNOWN_ERROR]; 
    75                 if($argument) 
    76                         $message = str_replace("%1", $argument, $message);               
    77                  
    78                 return array ("code" => $code, "message" => $message);  
     8        public static function getInstance(){ 
     9                if (!self::$sInstance) { 
     10                        self::$sInstance = new Errors(); 
     11                } 
     12                return self::$sInstance; 
    7913        } 
    8014         
    81         static public function getResponse($request, $responseException){                        
    82                         $response = new Response($request); 
    83                         $response->code = Response::OK; 
    84                         $response->addHeader('content-type', 'application/json'); 
    85                         if($request->id) 
    86                                 $body['id']     = $request->id; 
    87                          
    88                         $code    = $responseException->getCode(); 
    89                         $error = Errors::get($code);                     
    90                         $message = ($responseException->getMessage() ? $responseException->getMessage() : $error['message']);                    
    91                         $body['error'] = array("code" => $code, "message" => $message);          
    92                         $response->body = json_encode($body); 
    93                         return $response; 
     15        function __construct(){ 
     16                $this->errors = array(); 
     17                if($handle = fopen(__DIR__."/../../config/Errors.tsv", "r")){    
     18                        while (!feof($handle)) { 
     19                                $line = trim(fgets($handle,1024)); 
     20                                if($line == null || $line[0] == "#") 
     21                                        continue; 
     22                                $error = preg_split("/[\t]+/", $line); 
     23                                if(is_array($error)&& count($error) == 3) { 
     24                                        $this->errors[] = array("code" => $error[0], "key" => $error[1], "message" => $error[2]); 
     25                                } 
     26                        } 
     27                }        
    9428        } 
     29         
     30        static public function runException($needle, $argument = false){                 
     31                $error = false; 
     32                $type = (is_int($needle) ? "code" : "key"); 
     33                foreach( Errors::getInstance()->errors as $value ){ 
     34                        if($value[$type] == $needle){ 
     35                                $error = $value; 
     36                                break; 
     37                        } 
     38                } 
     39                 
     40                if($error['message'] && $argument){ 
     41                        $error['message'] = str_replace("%1", $argument, $error['message']); 
     42                }                
     43                if(!$error){ 
     44                        Errors::getInstance()->runException("E_UNKNOWN_ERROR"); 
     45                }                
     46                 
     47                throw new ResponseException($error['message'], $error['code']);           
     48        }        
    9549                 
    9650} 
  • sandbox/webservice/api/rest/catalog/ContactsResource.php

    r6148 r6165  
    2424                                                return $this->getGlobalContacts($search, $this->getParam('contactID')); 
    2525                                        else{ 
    26                                                 $error = Errors::get(Errors::CATALOG_MIN_ARGUMENT_SEARCH, $this-> getMinArgumentSearch());                                               
    27                                                 throw new ResponseException($error['message'], $error['code']); 
     26                                                Errors::runException("CATALOG_MIN_ARGUMENT_SEARCH", $this-> getMinArgumentSearch()); 
    2827                                        } 
    2928                                } 
  • sandbox/webservice/api/rest/core/LoginResource.php

    r6149 r6165  
    3535                else 
    3636                { 
    37                         throw new ResponseException("",$GLOBALS['phpgw']->session->cd_reason); 
     37                        Errors::runException($GLOBALS['phpgw']->session->cd_reason); 
    3838                } 
    3939                return $this->getResponse(); 
  • sandbox/webservice/api/rest/dispatch.php

    r6155 r6165  
    22 
    33// load libraries 
    4 require_once __DIR__.'/../library/tonic/lib/tonic.php';  
    5 require_once(__DIR__."/../Errors.php"); 
     4require_once(__DIR__.'/../library/tonic/lib/tonic.php');  
     5require_once(__DIR__.'/../library/utils/Errors.php'); 
    66 
    77// load adapters 
     
    5454                        break; 
    5555                default: 
    56                         $response = Errors::getResponse($request, $e); 
     56                         
     57                        $response = new Response($request); 
     58                        $response->code = Response::OK; 
     59                        $response->addHeader('content-type', 'application/json'); 
     60                        if($request->id) 
     61                                $body['id']     = $request->id; 
     62                         
     63                        $body['error'] = array("code" => "".$e->getCode(), "message" => $e->getMessage()); 
     64                        $response->body = json_encode($body); 
    5765        } 
    5866} 
  • sandbox/webservice/api/rest/mail/AddFolderResource.php

    r6155 r6165  
    1313 
    1414                        if(empty($new_name) || preg_match('/[\/\\\!\@\#\$\%\&\*\(\)]/', $new_name)) 
    15                                 throw new ResponseException("",Errors::MAIL_INVALID_NEW_FOLDER_NAME); 
     15                                Errors::runException("MAIL_INVALID_NEW_FOLDER_NAME"); 
    1616 
    1717                        $new_id = $parent_id . $this->getImap()->imap_delimiter . $new_name; 
     
    2121                        $result = $this->getImap()->create_mailbox($params); 
    2222                        if($result != 'Ok') 
    23                                 throw new ResponseException("",Errors::MAIL_FOLDER_NOT_ADDED); 
     23                                Errors::runException("MAIL_FOLDER_NOT_ADDED"); 
    2424                } 
    2525 
  • sandbox/webservice/api/rest/mail/CleanTrashResource.php

    r6148 r6165  
    1010                        $params['clean_folder'] = 'imapDefaultTrashFolder'; 
    1111                        if(!$this -> getImap() -> empty_folder($params)) 
    12                                 throw new ResponseException("",Errors::MAIL_TRASH_NOT_CLEANED); 
     12                                Errors::runException("MAIL_TRASH_NOT_CLEANED"); 
    1313                } 
    1414 
  • sandbox/webservice/api/rest/mail/DelFolderResource.php

    r6155 r6165  
    1111 
    1212                        if(!$this->getImap()->folder_exists($folder_id)) 
    13                                 throw new ResponseException("",Errors::MAIL_INVALID_FOLDER); 
     13                                Errors::runException("MAIL_INVALID_FOLDER"); 
    1414 
    1515                        $default_folders = array_keys($this->defaultFolders); 
    1616                        if(in_array($folder_id, $default_folders)) 
    17                                 throw new ResponseException("",Errors::MAIL_CANNOT_DEL_DEFAULT_FOLDER); 
     17                                Errors::runException("MAIL_CANNOT_DEL_DEFAULT_FOLDER"); 
    1818 
    1919                        $personal_folders = $this->getImap()->get_folders_list(array('noSharedFolders' => true, 'folderType' => 'personal')); 
     
    2323                        foreach($personal_folders AS $personal_folder){ 
    2424                                if($personal_folder['folder_id'] == $folder_id && $personal_folder['folder_hasChildren']) 
    25                                         throw new ResponseException("",Errors::MAIL_FOLDER_NOT_EMPTY); 
     25                                        Errors::runException("MAIL_FOLDER_NOT_EMPTY"); 
    2626                        } 
    2727 
    2828                        if($this->getImap()->get_num_msgs(array('folder' => $folder_id)) > 0) 
    29                                 throw new ResponseException("",Errors::MAIL_FOLDER_NOT_EMPTY); 
     29                                Errors::runException("MAIL_FOLDER_NOT_EMPTY"); 
    3030 
    3131                        $result = $this->getImap()->delete_mailbox($params); 
    3232                        if($result != 'Ok') 
    33                                 throw new ResponseException("",Errors::MAIL_FOLDER_NOT_DELETED); 
     33                                Errors::runException("MAIL_FOLDER_NOT_DELETED"); 
    3434                } 
    3535 
  • sandbox/webservice/api/rest/mail/RenameFolderResource.php

    r6155 r6165  
    1212 
    1313                        if(!$this->getImap()->folder_exists($old_id)) 
    14                                 throw new ResponseException("",Errors::MAIL_INVALID_OLD_FOLDER); 
     14                                Errors::runException("MAIL_INVALID_OLD_FOLDER"); 
    1515 
    1616                        $default_folders = array_keys($this->defaultFolders); 
    1717                        if(in_array($old_id, $default_folders)) 
    18                                 throw new ResponseException("",Errors::MAIL_INVALID_OLD_FOLDER); 
     18                                Errors::runException("MAIL_INVALID_OLD_FOLDER"); 
    1919 
    2020                        if(empty($new_name) || preg_match('/[\/\\\!\@\#\$\%\&\*\(\)]/', $new_name)) 
    21                                 throw new ResponseException("",Errors::MAIL_INVALID_NEW_FOLDER_NAME); 
     21                                Errors::runException("MAIL_INVALID_NEW_FOLDER_NAME"); 
    2222 
    2323                        $old_id_arr = explode($this->getImap()->imap_delimiter, $old_id); 
     
    3030                        $result = $this->getImap()->ren_mailbox($params); 
    3131                        if($result != 'Ok') 
    32                                 throw new ResponseException("",Errors::MAIL_FOLDER_NOT_RENAMED); 
     32                                Errors::runException("MAIL_FOLDER_NOT_RENAMED"); 
    3333                } 
    3434 
  • sandbox/webservice/api/rest/mail/SendSupportFeedbackResource.php

    r6148 r6165  
    2929 
    3030                        if (!$returncode || !(is_array($returncode) && $returncode['success'] == true)) 
    31                                 throw new ResponseException("",Errors::MAIL_NOT_SENT); 
     31                                Errors::runException("MAIL_NOT_SENT"); 
    3232                } 
    3333 
Note: See TracChangeset for help on using the changeset viewer.