Example #1
0
 /**
  * Returns an HTML-formatted string representation of the exception.
  *
  * @return string
  * @internal
  */
 public function __toString()
 {
     $msg = $this->getMessage();
     if (is_object($this->requestObj) && $this->requestObj instanceof \Guzzle\Http\Message\Request) {
         $request = array('url' => $this->requestObj->getUrl(), 'host' => $this->requestObj->getHost(), 'headers' => $this->requestObj->getRawHeaders(), 'query' => (string) $this->requestObj->getQuery());
         if ($this->requestObj instanceof \Guzzle\Http\Message\EntityEnclosingRequestInterface) {
             $request_body = $this->requestObj->getBody();
             $request['content-type'] = $request_body->getContentType();
             $request['content-length'] = $request_body->getContentLength();
             $request['body'] = $request_body->__toString();
         }
         $msg .= "\n\nRequest: <pre>" . htmlspecialchars(print_r($request, true)) . '</pre>';
     }
     if (is_object($this->responseObj) && $this->responseObj instanceof \Guzzle\Http\Message\Response) {
         $response = array('status' => $this->responseObj->getStatusCode(), 'headers' => $this->responseObj->getRawHeaders(), 'body' => $this->responseBody);
         $msg .= "\n\nResponse: <pre>" . htmlspecialchars(print_r($response, true)) . '</pre>';
     }
     return $msg;
 }
Example #2
0
 private function exec(\Guzzle\Http\Message\RequestInterface $request)
 {
     $start = microtime(true);
     $this->responseCode = 0;
     // Get snapshot of request headers.
     $request_headers = $request->getRawHeaders();
     // Mask authorization for logs.
     $request_headers = preg_replace('!\\nAuthorization: (Basic|Digest) [^\\r\\n]+\\r!i', "\nAuthorization: \$1 [**masked**]\r", $request_headers);
     try {
         $response = $request->send();
     } catch (\Guzzle\Http\Exception\BadResponseException $e) {
         $response = $e->getResponse();
     } catch (\Guzzle\Http\Exception\CurlException $e) {
         // Timeouts etc.
         DebugData::$raw = '';
         DebugData::$code = $e->getErrorNo();
         DebugData::$code_status = $e->getError();
         DebugData::$code_class = 0;
         DebugData::$exception = $e->getMessage();
         DebugData::$opts = array('request_headers' => $request_headers);
         DebugData::$data = null;
         $exception = new ResponseException($e->getError(), $e->getErrorNo(), $request->getUrl(), DebugData::$opts);
         $exception->requestObj = $request;
         // Log Exception
         $headers_array = $request->getHeaders();
         unset($headers_array['Authorization']);
         $headerString = '';
         foreach ($headers_array as $value) {
             $headerString .= $value->getName() . ': ' . $value . " ";
         }
         $log_message = '{code_status} ({code}) Request Details:[ {r_method} {r_resource} {r_scheme} {r_headers} ]';
         $httpScheme = strtoupper(str_replace('https', 'http', $request->getScheme())) . $request->getProtocolVersion();
         $log_params = array('code' => $e->getErrorNo(), 'code_status' => $e->getError(), 'r_method' => $request->getUrl(), 'r_resource' => $request->getRawHeaders(), 'r_scheme' => $httpScheme, 'r_headers' => $headerString);
         self::$logger->emergency($log_message, $log_params);
         throw $exception;
     }
     $this->responseCode = $response->getStatusCode();
     $this->responseText = trim($response->getBody(true));
     $this->responseLength = $response->getContentLength();
     $this->responseMimeType = $response->getContentType();
     $this->responseObj = array();
     $content_type = $response->getContentType();
     $firstChar = substr($this->responseText, 0, 1);
     if (strpos($content_type, '/json') !== false && ($firstChar == '{' || $firstChar == '[')) {
         $response_obj = @json_decode($this->responseText, true);
         if (is_array($response_obj)) {
             $this->responseObj = $response_obj;
         }
     }
     $status = self::getStatusMessage($this->responseCode);
     $code_class = floor($this->responseCode / 100);
     DebugData::$raw = $this->responseText;
     DebugData::$opts = array('request_headers' => $request_headers, 'response_headers' => $response->getRawHeaders());
     if ($request instanceof \Guzzle\Http\Message\EntityEnclosingRequestInterface) {
         DebugData::$opts['request_body'] = (string) $request->getBody();
     }
     DebugData::$opts['request_type'] = class_implements($request);
     DebugData::$data = $this->responseObj;
     DebugData::$code = $this->responseCode;
     DebugData::$code_status = $status;
     DebugData::$code_class = $code_class;
     DebugData::$exception = null;
     DebugData::$time_elapsed = microtime(true) - $start;
     if ($code_class != 2) {
         $uri = $request->getUrl();
         if (!empty($this->responseCode) && isset($this->responseObj['message'])) {
             $message = 'Code: ' . $this->responseCode . '; Message: ' . $this->responseObj['message'];
         } else {
             $message = 'API returned HTTP code of ' . $this->responseCode . ' when fetching from ' . $uri;
         }
         DebugData::$exception = $message;
         $this->debugCallback(DebugData::toArray());
         self::$logger->error($this->responseText);
         // Create better status to show up in logs
         $status .= ': ' . $request->getMethod() . ' ' . $uri;
         if ($request instanceof \Guzzle\Http\Message\EntityEnclosingRequestInterface) {
             $body = $request->getBody();
             if ($body instanceof \Guzzle\Http\EntityBodyInterface) {
                 $status .= ' with Content-Length of ' . $body->getContentLength() . ' and Content-Type of ' . $body->getContentType();
             }
         }
         $exception = new ResponseException($status, $this->responseCode, $uri, DebugData::$opts, $this->responseText);
         $exception->requestObj = $request;
         $exception->responseObj = $response;
         throw $exception;
     }
     $this->debugCallback(DebugData::toArray());
 }
Example #3
0
 /**
  * Get the raw message headers as a string
  *
  * @return string
  */
 public function getRawHeaders()
 {
     return $this->wrapped->getRawHeaders();
 }
Example #4
0
 /**
  * Throw a too many redirects exception for a request
  *
  * @param RequestInterface $request Request
  * @throws TooManyRedirectsException when too many redirects have been issued
  */
 protected function throwTooManyRedirectsException(RequestInterface $request)
 {
     $responses = array();
     // Create a nice message to use when throwing the exception that shows each request/response transaction
     do {
         $response = $request->getResponse();
         $responses[] = '> ' . $request->getRawHeaders() . "\n\n< " . $response->getRawHeaders();
         $request = $response->getPreviousResponse() ? $response->getPreviousResponse()->getRequest() : null;
     } while ($request);
     $transaction = implode("* Sending redirect request\n", array_reverse($responses));
     throw new TooManyRedirectsException("Too many redirects were issued for this transaction:\n{$transaction}");
 }