summaryrefslogtreecommitdiffstats
path: root/lib/SparkPost/SparkPostException.php
blob: bde2e5e8902da8b97a58d207c41e7df1b1729149 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?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;

    /**
     * Array with the request values sent.
     */
    private $request;

    /**
     * Sets up the custom exception and copies over original exception values.
     *
     * @param Exception $exception - the exception to be wrapped
     */
    public function __construct(\Exception $exception, $request = null)
    {
        $this->request = $request;

        $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 request values sent.
     *
     * @return array $request
     */
    public function getRequest()
    {
        return $this->request;
    }

    /**
     * Returns the body.
     *
     * @return array $body - the json decoded body from the http response
     */
    public function getBody()
    {
        return $this->body;
    }
}