Changeset 6165 for sandbox/webservice/api/library
- Timestamp:
- 05/11/12 10:38:02 (12 years ago)
- Location:
- sandbox/webservice/api/library/utils
- Files:
-
- 1 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
sandbox/webservice/api/library/utils/Errors.php
r6150 r6165 3 3 final class Errors { 4 4 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; 14 7 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; 79 13 } 80 14 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 } 94 28 } 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 } 95 49 96 50 }
Note: See TracChangeset
for help on using the changeset viewer.