Ejemplo n.º 1
0
 /**
  * 记录当前发生的错误内容,供getErrorxxxxx相关接口使用, 同时根据是否抛出异常的配置决定是否生成并抛出一个错误.
  * 
  * @param int $code 错误码, 非0表示有错误发生
  * 
  *                -1 未知错误
  *                 0 表示没有错误  
  *         1 ~   99 clientSDK错误
  *       400 ~   600 网络错误, http status code,  200-300为成功消息, 不可占用.
  *     10000 ~ 20000 服务端错误
  *     
  * @param string $requestId  请求ID, 由服务端生成并返回
  * @param int $code 错误码
  * @param string $msg 错误消息
  * @return Exception
  */
 protected function onError($requestId = null, $code = 0, $msg = '')
 {
     $this->errorMsg = $msg;
     $this->errorCode = $code;
     $this->requestId = $requestId;
     if ($code === 0) {
         // 无错误;
         return null;
     }
     if ($code > 0 && $code < 100) {
         // Http状态应小于100,所以认为小于100的均为SDK内部错误
         $type = self::ERR_CLIENT;
     } elseif ($code >= 400 && $code < 600) {
         // 只将Http的错误状态认为是网络错误. 200及300对于当前restApi来说将不能被理解.
         $type = self::ERR_NET;
     } elseif ($code >= 10000) {
         // 大于10000应为服务端返回的错误
         $type = self::ERR_SERVER;
     } else {
         $code = -1;
         $type = self::ERR_UNKNOWN;
     }
     // 生产错误
     $err = new $type("[{$code}] {$msg}", $code);
     if (self::$suppressException) {
         $this->log->err($err->__toString());
         return $err;
         // 拒绝抛出
     } else {
         throw $err;
     }
 }