From 4ec8817c4034d24603789ed47327dd19ffe77d76 Mon Sep 17 00:00:00 2001
From: Aaron Bauman <aaron@messageagency.com>
Date: Tue, 16 May 2017 10:53:13 -0400
Subject: [PATCH] Fixes fatal error when no response received from Salesforce

---
 src/Rest/RestClient.php    | 4 ++--
 src/Rest/RestException.php | 5 ++++-
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/Rest/RestClient.php b/src/Rest/RestClient.php
index 26a33763..e445b0e6 100644
--- a/src/Rest/RestClient.php
+++ b/src/Rest/RestClient.php
@@ -136,7 +136,7 @@ class RestClient implements RestClientInterface {
 
       // Any exceptions besides 401 get bubbled up.
       if (!$this->response || $this->response->getStatusCode() != 401) {
-        throw new RestException($this->response, $e->getMessage());
+        throw new RestException($this->response, $e->getMessage(), $e->getCode(), $e);
       }
     }
 
@@ -150,7 +150,7 @@ class RestClient implements RestClientInterface {
       }
       catch (RequestException $e) {
         $this->response = $e->getResponse();
-        throw new RestException($this->response, $e->getMessage());
+        throw new RestException($this->response, $e->getMessage(), $e->getCode(), $e);
       }
     }
 
diff --git a/src/Rest/RestException.php b/src/Rest/RestException.php
index 86f22495..4ada5216 100644
--- a/src/Rest/RestException.php
+++ b/src/Rest/RestException.php
@@ -15,7 +15,7 @@ class RestException extends \RuntimeException implements ExceptionInterface {
   /**
    *
    */
-  public function __construct(ResponseInterface $response, $message = "", $code = 0, Throwable $previous = NULL) {
+  public function __construct(ResponseInterface $response = NULL, $message = "", $code = 0, Throwable $previous = NULL) {
     $this->response = $response;
     $message .= $this->getResponseBody();
     parent::__construct($message, $code, $previous);
@@ -26,6 +26,9 @@ class RestException extends \RuntimeException implements ExceptionInterface {
   }
 
   public function getResponseBody() {
+    if (!$this->response) {
+      return;
+    }
     $body = $this->response->getBody();
     if ($body) {
       return $body->getContents();
-- 
GitLab