FoxyCart / foxyclient-php

FoxyClient - a PHP Guzzle based API Client for working with the Foxy Hypermedia API
MIT License
9 stars 11 forks source link

Improved exception handling #13

Closed sdrib closed 1 year ago

sdrib commented 5 years ago

The responses we currently get are not parseable and truncated.

Before:

"error_description" => """  
     Client error: `POST https://api.foxycart.com/carts/1569432898` resulted in a `422 Unprocessable Entity` response:\n
      {\n
          "total": 1,\n
          "_links": {\n
              "curies": [\n
                  {\n
                      "name": "fx",\n
                      "href" (truncated...)\n

After:

"error_description" => """
      Client error: `POST https://api.foxycart.com/carts/1569438348` resulted in a `422 Unprocessable Entity` response:\n
      {\n
          "total": 1,\n
          "_links": {\n
              "curies": [\n
                  {\n
                      "name": "fx",\n
                      "href" (truncated...)\n
      """
    "error_code" => 422
    "error_contents" => """
      {\n
          "total": 1,\n
          "_links": {\n
              "curies": [\n
                  {\n
                      "name": "fx",\n
                      "href": "https://api.foxycart.com/rels/{rel}",\n
                      "templated": true\n
                  }\n
              ]\n
          },\n
          "_embedded": {\n
              "fx:errors": [\n
                  {\n
                      "logref": "id-1554284141",\n
                      "message": "Errors:\nError: There was an error processing your payment: There was an issue with your order (general system failure). You can try again or contact us to complete your order. (Response Reason Code: 150)"\n
                  }\n
              ]\n
          }\n
      }
      """
adamjudd commented 4 years ago

@sdrib,

Sorry for the extended delay on reviewing your change here.

We've got a quick improvement to your change before we can merge, to account for situations (such as a curl exception) where the exception doesn't actually have a response. Something like this should account for that:

-    private function handleException(\Exception $e) 
+    private function handleException(\Exception $e)
     {
-        $response = $e->getResponse();
-
-        return array(
-            "error_description" => $e->getMessage() ,
-            "error_code" => $response->getStatusCode(),
-            "error_contents" => (string) $response->getBody()->getContents()
+        $error = array(
+            "error_description" => $e->getMessage()
+        );
+
+        if ($e->hasResponse()) {
+            $response = $e->getResponse();
+            $error = array_merge($error, array(
+                "error_code" => $response->getStatusCode(),
+                "error_contents" => (string) $response->getBody()->getContents()
+            ));
+        }
+
+        return $error;
     }

Did you want to update your master and we'll (finally) get this merged in?

sdrib commented 1 year ago

Done that just now @adamjudd 👍