Base.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Stripe\Error;
  3. use Exception;
  4. abstract class Base extends Exception
  5. {
  6. public function __construct(
  7. $message,
  8. $httpStatus = null,
  9. $httpBody = null,
  10. $jsonBody = null,
  11. $httpHeaders = null
  12. ) {
  13. parent::__construct($message);
  14. $this->httpStatus = $httpStatus;
  15. $this->httpBody = $httpBody;
  16. $this->jsonBody = $jsonBody;
  17. $this->httpHeaders = $httpHeaders;
  18. $this->requestId = null;
  19. // TODO: make this a proper constructor argument in the next major
  20. // release.
  21. $this->stripeCode = isset($jsonBody["error"]["code"]) ? $jsonBody["error"]["code"] : null;
  22. if ($httpHeaders && isset($httpHeaders['Request-Id'])) {
  23. $this->requestId = $httpHeaders['Request-Id'];
  24. }
  25. }
  26. public function getStripeCode()
  27. {
  28. return $this->stripeCode;
  29. }
  30. public function getHttpStatus()
  31. {
  32. return $this->httpStatus;
  33. }
  34. public function getHttpBody()
  35. {
  36. return $this->httpBody;
  37. }
  38. public function getJsonBody()
  39. {
  40. return $this->jsonBody;
  41. }
  42. public function getHttpHeaders()
  43. {
  44. return $this->httpHeaders;
  45. }
  46. public function getRequestId()
  47. {
  48. return $this->requestId;
  49. }
  50. public function __toString()
  51. {
  52. $id = $this->requestId ? " from API request '{$this->requestId}'": "";
  53. $message = explode("\n", parent::__toString());
  54. $message[0] .= $id;
  55. return implode("\n", $message);
  56. }
  57. }