1: <?php
 2: 
 3: /**
 4:  * Exception thrown when an API request fails
 5:  */
 6: class Syspay_Merchant_RequestException extends RuntimeException
 7: {
 8:     private $httpCode;
 9:     private $headers;
10:     private $body;
11: 
12:     public function __construct($httpCode, $headers = null, $body = null, $previous = null)
13:     {
14:         $this->httpCode = $httpCode;
15:         $this->headers  = $headers;
16:         $this->body     = $body;
17:         $message = '';
18:         $code = 0;
19:         // Look if the body contains an actual error
20:         if ($decoded = json_decode($this->body)) {
21:             if (isset($decoded->error) && ($decoded->error instanceof stdClass)) {
22:                 $code = $decoded->error->code;
23:                 $message = $decoded->error->message;
24:             }
25:         }
26:         if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
27:             parent::__construct($message, $code, $previous);
28:         } else {
29:             parent::__construct($message, $code);
30:         }
31:     }
32: 
33:     /**
34:      * Gets the response HTTP code
35:      * @return int HTTP code
36:      */
37:     public function getHttpCode()
38:     {
39:         return $this->httpCode;
40:     }
41: 
42:     /**
43:      * Gets the raw response headers
44:      * @return string HTTP headers
45:      */
46:     public function getHeaders()
47:     {
48:         return $this->headers;
49:     }
50: 
51:     /**
52:      * Gets the raw response body
53:      * @return string body
54:      */
55:     public function getBody()
56:     {
57:         return $this->body;
58:     }
59: }
60: