Exception.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Webapi module exception. Should be used in web API services implementation.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Webapi;
  9. use Magento\Framework\Exception\ErrorMessage;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Framework\Phrase;
  12. /**
  13. * Web API exception should not be used directly by any modules except for Magento_Webapi.
  14. *
  15. * During web API requests, all exceptions are converted to this exception,
  16. * which is then used for proper error response generation.
  17. */
  18. class Exception extends LocalizedException
  19. {
  20. /**#@+
  21. * Error HTTP response codes.
  22. */
  23. const HTTP_BAD_REQUEST = 400;
  24. const HTTP_UNAUTHORIZED = 401;
  25. const HTTP_FORBIDDEN = 403;
  26. const HTTP_NOT_FOUND = 404;
  27. const HTTP_METHOD_NOT_ALLOWED = 405;
  28. const HTTP_NOT_ACCEPTABLE = 406;
  29. const HTTP_INTERNAL_ERROR = 500;
  30. /**#@-*/
  31. /**#@+
  32. * Fault codes that are used in SOAP faults.
  33. */
  34. const FAULT_CODE_SENDER = 'Sender';
  35. const FAULT_CODE_RECEIVER = 'Receiver';
  36. /**
  37. * Optional exception details.
  38. *
  39. * @var array
  40. */
  41. protected $_details;
  42. /**
  43. * HTTP status code associated with current exception.
  44. *
  45. * @var int
  46. */
  47. protected $_httpCode;
  48. /**
  49. * Exception name is used for SOAP faults generation.
  50. *
  51. * @var string
  52. */
  53. protected $_name;
  54. /**
  55. * Stacktrace
  56. *
  57. * @var string
  58. */
  59. protected $_stackTrace;
  60. /**
  61. * List of errors
  62. *
  63. * @var null|\Magento\Framework\Exception\LocalizedException[]
  64. */
  65. protected $_errors;
  66. /**
  67. * Initialize exception with HTTP code.
  68. *
  69. * @param \Magento\Framework\Phrase $phrase
  70. * @param int $code Error code
  71. * @param int $httpCode
  72. * @param array $details Additional exception details
  73. * @param string $name Exception name
  74. * @param \Magento\Framework\Exception\LocalizedException[]|null $errors Array of errors messages
  75. * @param string $stackTrace
  76. *
  77. * @throws \InvalidArgumentException
  78. */
  79. public function __construct(
  80. Phrase $phrase,
  81. $code = 0,
  82. $httpCode = self::HTTP_BAD_REQUEST,
  83. array $details = [],
  84. $name = '',
  85. $errors = null,
  86. $stackTrace = null
  87. ) {
  88. /** Only HTTP error codes are allowed. No success or redirect codes must be used. */
  89. if ($httpCode < 400 || $httpCode > 599) {
  90. throw new \InvalidArgumentException(sprintf('The specified HTTP code "%d" is invalid.', $httpCode));
  91. }
  92. parent::__construct($phrase, null, $code);
  93. $this->code = $code;
  94. $this->_httpCode = $httpCode;
  95. $this->_details = $details;
  96. $this->_name = $name;
  97. $this->_errors = $errors;
  98. $this->_stackTrace = $stackTrace;
  99. }
  100. /**
  101. * Retrieve current HTTP code.
  102. *
  103. * @return int
  104. */
  105. public function getHttpCode()
  106. {
  107. return $this->_httpCode;
  108. }
  109. /**
  110. * Identify exception originator: sender or receiver.
  111. *
  112. * @return string
  113. */
  114. public function getOriginator()
  115. {
  116. return $this->getHttpCode() < 500 ? self::FAULT_CODE_SENDER : self::FAULT_CODE_RECEIVER;
  117. }
  118. /**
  119. * Retrieve exception details.
  120. *
  121. * @return array
  122. */
  123. public function getDetails()
  124. {
  125. return $this->_details;
  126. }
  127. /**
  128. * Retrieve exception name.
  129. *
  130. * @return string
  131. */
  132. public function getName()
  133. {
  134. return $this->_name;
  135. }
  136. /**
  137. * Retrieve list of errors.
  138. *
  139. * @return null|\Magento\Framework\Exception\LocalizedException[]
  140. */
  141. public function getErrors()
  142. {
  143. return $this->_errors;
  144. }
  145. /**
  146. * Retrieve stack trace string.
  147. *
  148. * @return null|string
  149. */
  150. public function getStackTrace()
  151. {
  152. return $this->_stackTrace;
  153. }
  154. }