| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,218 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+namespace Telnyx\Exception; |
|
| 4 |
+ |
|
| 5 |
+/** |
|
| 6 |
+ * Implements properties and methods common to all (non-SPL) Telnyx exceptions. |
|
| 7 |
+ */ |
|
| 8 |
+abstract class ApiErrorException extends \Exception implements ExceptionInterface |
|
| 9 |
+{
|
|
| 10 |
+ protected $error; |
|
| 11 |
+ protected $httpBody; |
|
| 12 |
+ protected $httpHeaders; |
|
| 13 |
+ protected $httpStatus; |
|
| 14 |
+ protected $jsonBody; |
|
| 15 |
+ protected $requestId; |
|
| 16 |
+ protected $telnyxCode; |
|
| 17 |
+ |
|
| 18 |
+ /** |
|
| 19 |
+ * Creates a new API error exception. |
|
| 20 |
+ * |
|
| 21 |
+ * @param string $message The exception message. |
|
| 22 |
+ * @param int|null $httpStatus The HTTP status code. |
|
| 23 |
+ * @param string|null $httpBody The HTTP body as a string. |
|
| 24 |
+ * @param array|null $jsonBody The JSON deserialized body. |
|
| 25 |
+ * @param array|\Telnyx\Util\CaseInsensitiveArray|null $httpHeaders The HTTP headers array. |
|
| 26 |
+ * @param string|null $telnyxCode The Telnyx error code. |
|
| 27 |
+ * |
|
| 28 |
+ * @return static |
|
| 29 |
+ */ |
|
| 30 |
+ public static function factory( |
|
| 31 |
+ $message, |
|
| 32 |
+ $httpStatus = null, |
|
| 33 |
+ $httpBody = null, |
|
| 34 |
+ $jsonBody = null, |
|
| 35 |
+ $httpHeaders = null, |
|
| 36 |
+ $telnyxCode = null |
|
| 37 |
+ ) {
|
|
| 38 |
+ $instance = new static($message); |
|
| 39 |
+ $instance->setHttpStatus($httpStatus); |
|
| 40 |
+ $instance->setHttpBody($httpBody); |
|
| 41 |
+ $instance->setJsonBody($jsonBody); |
|
| 42 |
+ $instance->setHttpHeaders($httpHeaders); |
|
| 43 |
+ $instance->setTelnyxCode($telnyxCode); |
|
| 44 |
+ $instance->setRequestId(null); |
|
| 45 |
+ if ($httpHeaders && isset($httpHeaders['Request-Id'])) {
|
|
| 46 |
+ $instance->setRequestId($httpHeaders['Request-Id']); |
|
| 47 |
+ } |
|
| 48 |
+ |
|
| 49 |
+ $instance->setError($instance->constructErrorObject()); |
|
| 50 |
+ |
|
| 51 |
+ return $instance; |
|
| 52 |
+ } |
|
| 53 |
+ |
|
| 54 |
+ /** |
|
| 55 |
+ * Gets the Telnyx error object. |
|
| 56 |
+ * |
|
| 57 |
+ * @return \Telnyx\ErrorObject|null |
|
| 58 |
+ */ |
|
| 59 |
+ public function getError() |
|
| 60 |
+ {
|
|
| 61 |
+ return $this->errors; |
|
| 62 |
+ } |
|
| 63 |
+ |
|
| 64 |
+ /** |
|
| 65 |
+ * Sets the Telnyx error object. |
|
| 66 |
+ * |
|
| 67 |
+ * @param \Telnyx\ErrorObject|null $error |
|
| 68 |
+ */ |
|
| 69 |
+ public function setError($errors) |
|
| 70 |
+ {
|
|
| 71 |
+ $this->errors = $errors; |
|
| 72 |
+ } |
|
| 73 |
+ |
|
| 74 |
+ /** |
|
| 75 |
+ * Gets the HTTP body as a string. |
|
| 76 |
+ * |
|
| 77 |
+ * @return string|null |
|
| 78 |
+ */ |
|
| 79 |
+ public function getHttpBody() |
|
| 80 |
+ {
|
|
| 81 |
+ return $this->httpBody; |
|
| 82 |
+ } |
|
| 83 |
+ |
|
| 84 |
+ /** |
|
| 85 |
+ * Sets the HTTP body as a string. |
|
| 86 |
+ * |
|
| 87 |
+ * @param string|null $httpBody |
|
| 88 |
+ */ |
|
| 89 |
+ public function setHttpBody($httpBody) |
|
| 90 |
+ {
|
|
| 91 |
+ $this->httpBody = $httpBody; |
|
| 92 |
+ } |
|
| 93 |
+ |
|
| 94 |
+ /** |
|
| 95 |
+ * Gets the HTTP headers array. |
|
| 96 |
+ * |
|
| 97 |
+ * @return array|\Telnyx\Util\CaseInsensitiveArray|null |
|
| 98 |
+ */ |
|
| 99 |
+ public function getHttpHeaders() |
|
| 100 |
+ {
|
|
| 101 |
+ return $this->httpHeaders; |
|
| 102 |
+ } |
|
| 103 |
+ |
|
| 104 |
+ /** |
|
| 105 |
+ * Sets the HTTP headers array. |
|
| 106 |
+ * |
|
| 107 |
+ * @param array|\Telnyx\Util\CaseInsensitiveArray|null $httpHeaders |
|
| 108 |
+ */ |
|
| 109 |
+ public function setHttpHeaders($httpHeaders) |
|
| 110 |
+ {
|
|
| 111 |
+ $this->httpHeaders = $httpHeaders; |
|
| 112 |
+ } |
|
| 113 |
+ |
|
| 114 |
+ /** |
|
| 115 |
+ * Gets the HTTP status code. |
|
| 116 |
+ * |
|
| 117 |
+ * @return int|null |
|
| 118 |
+ */ |
|
| 119 |
+ public function getHttpStatus() |
|
| 120 |
+ {
|
|
| 121 |
+ return $this->httpStatus; |
|
| 122 |
+ } |
|
| 123 |
+ |
|
| 124 |
+ /** |
|
| 125 |
+ * Sets the HTTP status code. |
|
| 126 |
+ * |
|
| 127 |
+ * @param int|null $httpStatus |
|
| 128 |
+ */ |
|
| 129 |
+ public function setHttpStatus($httpStatus) |
|
| 130 |
+ {
|
|
| 131 |
+ $this->httpStatus = $httpStatus; |
|
| 132 |
+ } |
|
| 133 |
+ |
|
| 134 |
+ /** |
|
| 135 |
+ * Gets the JSON deserialized body. |
|
| 136 |
+ * |
|
| 137 |
+ * @return array|null |
|
| 138 |
+ */ |
|
| 139 |
+ public function getJsonBody() |
|
| 140 |
+ {
|
|
| 141 |
+ return $this->jsonBody; |
|
| 142 |
+ } |
|
| 143 |
+ |
|
| 144 |
+ /** |
|
| 145 |
+ * Sets the JSON deserialized body. |
|
| 146 |
+ * |
|
| 147 |
+ * @param array|null $jsonBody |
|
| 148 |
+ */ |
|
| 149 |
+ public function setJsonBody($jsonBody) |
|
| 150 |
+ {
|
|
| 151 |
+ $this->jsonBody = $jsonBody; |
|
| 152 |
+ } |
|
| 153 |
+ |
|
| 154 |
+ /** |
|
| 155 |
+ * Gets the Telnyx request ID. |
|
| 156 |
+ * |
|
| 157 |
+ * @return string|null |
|
| 158 |
+ */ |
|
| 159 |
+ public function getRequestId() |
|
| 160 |
+ {
|
|
| 161 |
+ return $this->requestId; |
|
| 162 |
+ } |
|
| 163 |
+ |
|
| 164 |
+ /** |
|
| 165 |
+ * Sets the Telnyx request ID. |
|
| 166 |
+ * |
|
| 167 |
+ * @param string|null $requestId |
|
| 168 |
+ */ |
|
| 169 |
+ public function setRequestId($requestId) |
|
| 170 |
+ {
|
|
| 171 |
+ $this->requestId = $requestId; |
|
| 172 |
+ } |
|
| 173 |
+ |
|
| 174 |
+ /** |
|
| 175 |
+ * Gets the Telnyx error code. |
|
| 176 |
+ * |
|
| 177 |
+ * Cf. the `CODE_*` constants on {@see \Telnyx\ErrorObject} for possible
|
|
| 178 |
+ * values. |
|
| 179 |
+ * |
|
| 180 |
+ * @return string|null |
|
| 181 |
+ */ |
|
| 182 |
+ public function getTelnyxCode() |
|
| 183 |
+ {
|
|
| 184 |
+ return $this->telnyxCode; |
|
| 185 |
+ } |
|
| 186 |
+ |
|
| 187 |
+ /** |
|
| 188 |
+ * Sets the Telnyx error code. |
|
| 189 |
+ * |
|
| 190 |
+ * @param string|null $telnyxCode |
|
| 191 |
+ */ |
|
| 192 |
+ public function setTelnyxCode($telnyxCode) |
|
| 193 |
+ {
|
|
| 194 |
+ $this->telnyxCode = $telnyxCode; |
|
| 195 |
+ } |
|
| 196 |
+ |
|
| 197 |
+ /** |
|
| 198 |
+ * Returns the string representation of the exception. |
|
| 199 |
+ * |
|
| 200 |
+ * @return string |
|
| 201 |
+ */ |
|
| 202 |
+ public function __toString() |
|
| 203 |
+ {
|
|
| 204 |
+ $statusStr = ($this->getHttpStatus() == null) ? "" : "(HTTP {$this->getHttpStatus()}) ";
|
|
| 205 |
+ $idStr = ($this->getRequestId() == null) ? "" : "(Request {$this->getRequestId()}) ";
|
|
| 206 |
+ $codeStr = ($this->getTelnyxCode() == null) ? "" : "(Code {$this->getTelnyxCode()}) ";
|
|
| 207 |
+ return "Telnyx API Exception {$statusStr}{$codeStr}{$idStr}{$this->getMessage()}";
|
|
| 208 |
+ } |
|
| 209 |
+ |
|
| 210 |
+ protected function constructErrorObject() |
|
| 211 |
+ {
|
|
| 212 |
+ if (is_null($this->jsonBody) || !array_key_exists('errors', $this->jsonBody)) {
|
|
| 213 |
+ return null; |
|
| 214 |
+ } |
|
| 215 |
+ |
|
| 216 |
+ return \Telnyx\ErrorObject::constructFrom($this->jsonBody['errors'][0]); |
|
| 217 |
+ } |
|
| 218 |
+} |