From 4248f6881aeb3ba84735b437a9378805b010570a Mon Sep 17 00:00:00 2001 From: Scott Rogers Date: Thu, 11 Aug 2016 23:36:27 -0300 Subject: [PATCH] Added details to exception Added the details in a formatted message when throwing an exception. Done to return pertinent information to the caller on why the call failed. --- .../communications/HttpConnector.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Beanstream/communications/HttpConnector.php b/src/Beanstream/communications/HttpConnector.php index bd349fd..93f54a0 100644 --- a/src/Beanstream/communications/HttpConnector.php +++ b/src/Beanstream/communications/HttpConnector.php @@ -115,7 +115,24 @@ private function request($http_method = NULL, $url, $data = NULL) //check for return errors from the API if (isset($res['code']) && 1 < $res['code'] && !($req['http_code'] >= 200 && $req['http_code'] < 300)) { - throw new ApiException($res['message'], $res['code']); + // Store the base exception message to return + $messageText = $res['message']; + + // If there any any details messages, return them to the caller. + if (count($res["details"])) + { + $messageText .= " ("; + foreach ($res["details"] as $idx => $message) { + $messageText .= $message["message"]; + $messageText .= ", "; + } + + // Remove trailing comma + $messageText = rtrim($messageText, ", "); + $messageText .= ")"; + + } + throw new ApiException($messageText, $res['code']); } return $res;