summaryrefslogtreecommitdiffstats
path: root/lib/SparkPost/SparkPostException.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/SparkPost/SparkPostException.php')
-rw-r--r--lib/SparkPost/SparkPostException.php41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/SparkPost/SparkPostException.php b/lib/SparkPost/SparkPostException.php
new file mode 100644
index 0000000..ca92e24
--- /dev/null
+++ b/lib/SparkPost/SparkPostException.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace SparkPost;
+
+use Http\Client\Exception\HttpException as HttpException;
+
+class SparkPostException extends \Exception
+{
+ /**
+ * Variable to hold json decoded body from http response.
+ */
+ private $body = null;
+
+ /**
+ * Sets up the custom exception and copies over original exception values.
+ *
+ * @param Exception $exception - the exception to be wrapped
+ */
+ public function __construct(\Exception $exception)
+ {
+ $message = $exception->getMessage();
+ $code = $exception->getCode();
+ if ($exception instanceof HttpException) {
+ $message = $exception->getResponse()->getBody()->__toString();
+ $this->body = json_decode($message, true);
+ $code = $exception->getResponse()->getStatusCode();
+ }
+
+ parent::__construct($message, $code, $exception->getPrevious());
+ }
+
+ /**
+ * Returns the body.
+ *
+ * @return array $body - the json decoded body from the http response
+ */
+ public function getBody()
+ {
+ return $this->body;
+ }
+}