Inheritance: extends Exception
Example #1
0
 private static function __doCurl($url, $data, $additionalHeaders)
 {
     $curlHandle = curl_init($url);
     if ($curlHandle === FALSE) {
         $exception = new ApiException("Failed to connect to url {$url}", Config::$LOG_LEVEL_ERROR_KEY);
         $exception->setAdditionalInfo("source", "ApiCurl::__doCurl");
         $exception->setAdditionalInfo("url", $url);
         throw $exception;
     }
     if (!empty($data)) {
         curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $data);
         curl_setopt($curlHandle, CURLOPT_POST, 1);
     }
     curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, 1);
     curl_setopt($curlHandle, CURLOPT_MAXREDIRS, Config::$CURL_MAX_REDIRECTS);
     curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, Config::$CURL_CONNECTION_TIMEOUT);
     if (!empty($additionalHeaders)) {
         foreach ($additionalHeaders as $key => $value) {
             curl_setopt($curlHandle, $key, $value);
         }
     }
     $result = curl_exec($curlHandle);
     curl_close($curlHandle);
     if ($result === FALSE) {
         $exception = new ApiException("Failed to exec curl to {$url}", Config::$LOG_LEVEL_ERROR_KEY);
         $exception->setAdditionalInfo("source", "ApiCurl::__doCurl");
         $exception->setAdditionalInfo("url", $url);
         $exception->setAdditionalInfo("data", $data);
         $exception->setAdditionalInfo("additionalHeaders", $additionalHeaders);
         throw $exception;
     }
     return $result;
 }
Example #2
0
 public static function delete($key)
 {
     unset($_COOKIE[$key]);
     if (!setcookie($key, NULL, time() - 1, "/")) {
         $exception = new ApiException("Failed to set cookie {$key}", Config::$LOG_LEVEL_WARNING_KEY);
         $exception->setAdditionalInfo("source", "ApiCookies::delete");
         $exception->setAdditionalInfo("key", $key);
         throw $exception;
     }
 }
 public function __construct($mysqlAttributeErrorNo, $mysqlAttributeErrorMessage, $previous = null)
 {
     $this->errNo = $mysqlAttributeErrorNo;
     $this->error = $mysqlAttributeErrorMessage;
     $customMessage = "Error => " . $this->errNo . ": " . $this->error;
     parent::__construct('902', $customMessage, $previous);
 }
 /**
  * Resource methods
  */
 public function post()
 {
     // Just a dummy redirect to prevent errors in checkout/reward/reward.
     $this->request->post['redirect'] = 'checkout/cart';
     $data = parent::getInternalRouteData('checkout/reward/reward', true);
     ApiException::evaluateErrors($data);
 }
 public function __construct($request, $status_code, $description, Exception $previous = null)
 {
     $this->request = $request;
     $this->status_code = $status_code;
     $this->description = $description;
     parent::__construct($description, $status_code, $previous);
 }
 /**
  * Resource methods
  */
 public function post()
 {
     $data = parent::getInternalRouteData('tool/upload', true);
     ApiException::evaluateErrors($data);
     $file = array("file" => $this->getFile($data));
     $this->response->setOutput($file);
 }
 public function __construct($errorNo, $errorMessage, $previous = null)
 {
     $this->errNo = $errorNo;
     $this->error = $errorMessage;
     $customMessage = "Error => " . $this->errNo . ": " . $this->error;
     parent::__construct($this->errorNo, $customMessage, $previous);
 }
 /**
  * Resource methods
  */
 public function get()
 {
     $data = parent::getInternalRouteData('checkout/cart');
     ApiException::evaluateErrors($data, false);
     $cart = array('cart' => $this->getCart($data));
     $this->response->setOutput($cart);
 }
 /**
  * Resource methods
  */
 public function put($id = NULL)
 {
     $this->request->convertBoolToYesNoRadioValue('newsletter');
     $this->request->server['REQUEST_METHOD'] = 'POST';
     $data = parent::getInternalRouteData('account/newsletter');
     ApiException::evaluateErrors($data);
 }
 /**
  * Resource methods
  */
 public function post()
 {
     $this->request->setDefaultParameters($this->defaultParameters);
     $this->request->convertBoolToCheckbox('agree');
     $this->request->convertBoolToYesNoRadioValue('newsletter');
     $data = parent::getInternalRouteData('account/register');
     ApiException::evaluateErrors($data);
 }
 /**
  * Resource methods
  */
 public function post()
 {
     $this->user->logout();
     unset($this->session->data['token']);
     $data = array('success' => true);
     $this->response->setOutput($data);
     ApiException::evaluateErrors($data);
 }
 public function delete($id = NULL)
 {
     if ($id !== NULL) {
         $this->request->get['remove'] = $id;
     }
     $data = parent::getInternalRouteData('account/wishlist');
     ApiException::evaluateErrors($data);
 }
 public function post()
 {
     $this->request->setDefaultParameters($this->defaultParameters);
     $data = parent::getInternalRouteData('checkout/shipping_method/save', true);
     if (isset($data['redirect'])) {
         $this->redirect($data['redirect']);
     }
     ApiException::evaluateErrors($data);
 }
 public function put()
 {
     $this->request->setDefaultParameters($this->defaultParameters);
     $this->request->server['REQUEST_METHOD'] = 'POST';
     $data = parent::getInternalRouteData('account/edit');
     ApiException::evaluateErrors($data);
     $account = array('account' => $this->getAccount($data));
     $this->response->setOutput($account);
 }
 /**
  * Resource methods
  */
 public function get()
 {
     $data = parent::getInternalRouteData('checkout/confirm');
     if (isset($data['redirect'])) {
         $this->redirect($data['redirect']);
     }
     ApiException::evaluateErrors($data);
     $order = array('order' => $this->getOrder($data));
     $this->response->setOutput($order);
 }
 /**
  * Resource methods
  */
 public function post()
 {
     $this->request->setDefaultParameters($this->defaultParameters);
     $this->request->convertBoolToCheckbox('shipping_address');
     $data = parent::getInternalRouteData('checkout/guest/save', true);
     if (isset($data['redirect'])) {
         $this->redirect($data['redirect']);
     }
     ApiException::evaluateErrors($data);
 }
Example #17
0
 public function __construct($mongoException, $previous = null)
 {
     $errorCode = $customMessage = null;
     if ($mongoException instanceof MongoDuplicateKeyException) {
         throw new DuplicateEntityException();
     } else {
         $errorCode = '800';
         $customMessage = "Exception: Type => " . get_class($mongoException) . ": " . $mongoException->getMessage();
     }
     parent::__construct($errorCode, $customMessage, $previous);
 }
 public function delete($id)
 {
     $cartItemKeys = explode(',', $id);
     foreach ($cartItemKeys as $cartItemKey) {
         $this->request->post['key'] = $cartItemKey;
         $data = parent::getInternalRouteData('checkout/cart/remove', true);
         ApiException::evaluateErrors($data);
     }
     // Return cart
     $this->request->post = array();
     $this->get();
 }
Example #19
0
 public function __construct($mysqlErrorNo, $mysqlErrorMessage, $previous = null)
 {
     $this->errNo = $mysqlErrorNo;
     $this->error = $mysqlErrorMessage;
     $errorCode = '900';
     if ($mysqlErrorNo == '1062') {
         throw new DuplicateEntityException();
     } else {
         $customMessage = "MySQL Error => " . $this->errNo . ": " . $this->error;
     }
     parent::__construct($errorCode, $customMessage, $previous);
 }
Example #20
0
 private static function __sendMail($sender, $receivers, $subject, $body, $replyTo, $ccs, $bccs, $contentType)
 {
     $replyTo = self::__parseField(empty($replyTo) ? $sender : $replyTo);
     $sender = self::__parseField($sender);
     $receivers = self::__parseField($receivers);
     $ccs = self::__parseField($ccs);
     $bccs = self::__parseField($bccs);
     $headers = self::__composeHeaders($sender["forHeaders"], $receivers["forHeaders"], $subject, $replyTo["forHeaders"], $ccs["forHeaders"], $bccs["forHeaders"], $contentType);
     if (!mail($receivers["forEmail"], $subject, $body, $headers)) {
         $exception = new ApiException("Failed to send email", Config::$LOG_LEVEL_WARNING_KEY);
         $exception->setAdditionalInfo("source", "ApiMail::__sendMail");
         $exception->setAdditionalInfo("sender", $sender);
         $exception->setAdditionalInfo("receivers", $receivers);
         $exception->setAdditionalInfo("subject", $subject);
         $exception->setAdditionalInfo("body", $body);
         $exception->setAdditionalInfo("replyTo", $replyTo);
         $exception->setAdditionalInfo("ccs", $ccs);
         $exception->setAdditionalInfo("bccs", $bccs);
         $exception->setAdditionalInfo("contentType", $contentType);
         throw $exception;
     }
 }
 /**
  * Resource methods
  */
 public function post()
 {
     // Validate if customer is logged in. This is needed because in the parent::validate the checkout/checkout route serves two purposes.
     if (!$this->customer->isLogged()) {
         throw new ApiException(ApiResponse::HTTP_RESPONSE_CODE_BAD_REQUEST, ErrorCodes::ERRORCODE_USER_NOT_LOGGED_IN, ErrorCodes::getMessage(ErrorCodes::ERRORCODE_USER_NOT_LOGGED_IN));
     }
     $this->request->setDefaultParameters($this->defaultParameters);
     $data = parent::getInternalRouteData('checkout/shipping_address/save', true);
     if (isset($data['redirect'])) {
         $this->redirect($data['redirect']);
     }
     ApiException::evaluateErrors($data);
 }
 /**
  * Resource methods
  */
 public function post()
 {
     $user = $this->user;
     $data = array();
     if (isset($this->request->post['username']) && isset($this->request->post['username']) && $user->login($this->request->post['username'], $this->request->post['password'])) {
         $data['user'] = array('username' => $user->getUserName(), 'user_id' => $user->getId(), 'user_group_id' => $user->getGroupId(), 'vendor_id' => $user->getVP());
     } else {
         // Need to keep the static messages in a separate file (Keep it in API end ?)
         $data['errors'] = array('code' => "error_warning", 'message' => "Warning: No match for Username and/or Password.");
     }
     $this->response->setOutput($data);
     ApiException::evaluateErrors($data);
 }
 private static function findErrors($error, $keyPrefix, $apiExceptionObject, $warningIsError = true)
 {
     if (is_array($error)) {
         foreach ($error as $errorKey => $errorValue) {
             if (!($warningIsError === false && $errorKey == 'warning')) {
                 $key = $keyPrefix . '_' . $errorKey;
                 ApiException::findErrors($errorValue, $key, $apiExceptionObject, $warningIsError);
             }
         }
     } else {
         $apiExceptionObject->addError($keyPrefix, $error);
     }
 }
 public function getRecurringDescription($id = NULL)
 {
     $this->request->post['product_id'] = $id;
     $this->request->post['recurring_id'] = $this->request->get['recurring_id'];
     if (isset($this->request->get['quantity'])) {
         $this->request->post['quantity'] = $this->request->get['quantity'];
     }
     $data = parent::getInternalRouteData('product/product/getRecurringDescription', true);
     ApiException::evaluateErrors($data);
     if (isset($data['success'])) {
         $product = array('recurring_description' => $data['success']);
         $this->response->setOutput($product);
     } else {
         // No description found.
         throw new ApiException(ApiResponse::HTTP_RESPONSE_CODE_NOT_FOUND, ErrorCodes::ERRORCODE_RECURRING_DESCRIPTION_NOT_FOUND, ErrorCodes::getMessage(ErrorCodes::ERRORCODE_RECURRING_DESCRIPTION_NOT_FOUND));
     }
 }
Example #25
0
 /**
  * Resource methods
  */
 public function post()
 {
     $user = $this->user;
     $data = array();
     if ($this->user->isLogged()) {
         if (!$this->user->hasPermission('modify', 'user/vdi_user_password')) {
             throw new ApiException(ApiResponse::HTTP_RESPONSE_CODE_BAD_REQUEST, ErrorCodes::ERRORCODE_VENDOR_NOT_ALLOWED, "not allowed");
         }
         //log in to check old password
         if (!$user->login($this->user->getUserName(), $this->request->post['old_password'])) {
             throw new ApiException(ApiResponse::HTTP_RESPONSE_CODE_UNAUTHORIZED, ErrorCodes::ERRORCODE_USER_NOT_LOGGED_IN, "error confirming old password");
         }
         //now set new password
         if ($this->user->getId()) {
             $this->load->model('user/vdi_user_password');
             $this->model_user_vdi_user_password->editPassword($this->user->getId(), $this->request->post['new_password']);
             $data = array('success' => true);
         }
     } else {
         throw new ApiException(ApiResponse::HTTP_RESPONSE_CODE_UNAUTHORIZED, ErrorCodes::ERRORCODE_USER_NOT_LOGGED_IN, "user not logged in");
     }
     $this->response->setOutput($data);
     ApiException::evaluateErrors($data);
 }
 function __construct($channelAttributeType, $channelAttributeValue, $previous = null)
 {
     parent::__construct('7006', isset($channelAttributeValue) ? "Channel with " . "{$channelAttributeType} '{$channelAttributeValue}' does not exist" : "", $previous);
 }
 /**
  * Resource methods
  */
 public function post()
 {
     $data = parent::getInternalRouteData('account/forgotten');
     ApiException::evaluateErrors($data);
 }
Example #28
0
 /**
  * Make the HTTP call (Sync)
  *
  * @param string $resourcePath path to method endpoint
  * @param string $method       method to call
  * @param array  $queryParams  parameters to be place in query URL
  * @param array  $postData     parameters to be placed in POST body
  * @param array  $headerParams parameters to be place in request header
  * @param string $responseType expected response type of the endpoint
  * @param string $endpointPath path to method endpoint before expanding parameters
  *
  * @throws \Squiggle\ApiException on a non 2xx response
  * @return mixed
  */
 public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams, $responseType = null, $endpointPath = null)
 {
     $headers = [];
     // construct the http header
     $headerParams = array_merge((array) $this->config->getDefaultHeaders(), (array) $headerParams);
     foreach ($headerParams as $key => $val) {
         $headers[] = "{$key}: {$val}";
     }
     // form data
     if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers, true)) {
         $postData = http_build_query($postData);
     } elseif ((is_object($postData) or is_array($postData)) and !in_array('Content-Type: multipart/form-data', $headers, true)) {
         // json model
         $postData = json_encode(\Squiggle\ObjectSerializer::sanitizeForSerialization($postData));
     }
     $url = $this->config->getHost() . $resourcePath;
     $curl = curl_init();
     // set timeout, if needed
     if ($this->config->getCurlTimeout() !== 0) {
         curl_setopt($curl, CURLOPT_TIMEOUT, $this->config->getCurlTimeout());
     }
     // return the result on success, rather than just true
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     // disable SSL verification, if needed
     if ($this->config->getSSLVerification() === false) {
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
     }
     if (!empty($queryParams)) {
         $url = $url . '?' . http_build_query($queryParams);
     }
     if ($method === self::$POST) {
         curl_setopt($curl, CURLOPT_POST, true);
         curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
     } elseif ($method === self::$HEAD) {
         curl_setopt($curl, CURLOPT_NOBODY, true);
     } elseif ($method === self::$OPTIONS) {
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "OPTIONS");
         curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
     } elseif ($method === self::$PATCH) {
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
         curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
     } elseif ($method === self::$PUT) {
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
         curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
     } elseif ($method === self::$DELETE) {
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
         curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
     } elseif ($method !== self::$GET) {
         throw new ApiException('Method ' . $method . ' is not recognized.');
     }
     curl_setopt($curl, CURLOPT_URL, $url);
     // Set user agent
     curl_setopt($curl, CURLOPT_USERAGENT, $this->config->getUserAgent());
     // debugging for curl
     if ($this->config->getDebug()) {
         error_log("[DEBUG] HTTP Request body  ~BEGIN~" . PHP_EOL . print_r($postData, true) . PHP_EOL . "~END~" . PHP_EOL, 3, $this->config->getDebugFile());
         curl_setopt($curl, CURLOPT_VERBOSE, 1);
         curl_setopt($curl, CURLOPT_STDERR, fopen($this->config->getDebugFile(), 'a'));
     } else {
         curl_setopt($curl, CURLOPT_VERBOSE, 0);
     }
     // obtain the HTTP response headers
     curl_setopt($curl, CURLOPT_HEADER, 1);
     // Make the request
     $response = curl_exec($curl);
     $http_header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
     $http_header = $this->httpParseHeaders(substr($response, 0, $http_header_size));
     $http_body = substr($response, $http_header_size);
     $response_info = curl_getinfo($curl);
     // debug HTTP response body
     if ($this->config->getDebug()) {
         error_log("[DEBUG] HTTP Response body ~BEGIN~" . PHP_EOL . print_r($http_body, true) . PHP_EOL . "~END~" . PHP_EOL, 3, $this->config->getDebugFile());
     }
     // Handle the response
     if ($response_info['http_code'] === 0) {
         $curl_error_message = curl_error($curl);
         // curl_exec can sometimes fail but still return a blank message from curl_error().
         if (!empty($curl_error_message)) {
             $error_message = "API call to {$url} failed: {$curl_error_message}";
         } else {
             $error_message = "API call to {$url} failed, but for an unknown reason. " . "This could happen if you are disconnected from the network.";
         }
         $exception = new ApiException($error_message, 0, null, null);
         $exception->setResponseObject($response_info);
         throw $exception;
     } elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299) {
         // return raw body if response is a file
         if ($responseType === '\\SplFileObject' || $responseType === 'string') {
             return [$http_body, $response_info['http_code'], $http_header];
         }
         $data = json_decode($http_body);
         if (json_last_error() > 0) {
             // if response is a string
             $data = $http_body;
         }
     } else {
         $data = json_decode($http_body);
         if (json_last_error() > 0) {
             // if response is a string
             $data = $http_body;
         }
         throw new ApiException("[" . $response_info['http_code'] . "] Error connecting to the API ({$url})", $response_info['http_code'], $http_header, $data);
     }
     if (is_object($data) && property_exists($data, 'data')) {
         $data = $data->data;
     }
     return [$data, $response_info['http_code'], $http_header];
 }
 public function __construct($message = 'Deprecated')
 {
     parent::__construct($message, 405);
 }
 function __construct($orgDataFieldAttributeType = null, $orgDataFieldAttributeValue = null, $previous = null)
 {
     parent::__construct('6204', isset($orgDataFieldAttributeValue) ? "Org-channel-field with " . "{$orgDataFieldAttributeType} '{$orgDataFieldAttributeValue}' does not exist" : "", $previous);
 }