ApiErrorException.php 4.9 KB

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