RestException.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Luracast\Restler;
  3. use Exception;
  4. /**
  5. * Special Exception for raising API errors
  6. * that can be used in API methods
  7. *
  8. * @category Framework
  9. * @package Restler
  10. * @subpackage exception
  11. * @author R.Arul Kumaran <arul@luracast.com>
  12. * @copyright 2010 Luracast
  13. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  14. * @link http://luracast.com/products/restler/
  15. *
  16. */
  17. class RestException extends Exception
  18. {
  19. /**
  20. * HTTP status codes
  21. *
  22. * @var array
  23. */
  24. public static $codes = array(
  25. 100 => 'Continue',
  26. 101 => 'Switching Protocols',
  27. 200 => 'OK',
  28. 201 => 'Created',
  29. 202 => 'Accepted',
  30. 203 => 'Non-Authoritative Information',
  31. 204 => 'No Content',
  32. 205 => 'Reset Content',
  33. 206 => 'Partial Content',
  34. 300 => 'Multiple Choices',
  35. 301 => 'Moved Permanently',
  36. 302 => 'Found',
  37. 303 => 'See Other',
  38. 304 => 'Not Modified',
  39. 305 => 'Use Proxy',
  40. 306 => '(Unused)',
  41. 307 => 'Temporary Redirect',
  42. 400 => 'Bad Request',
  43. 401 => 'Unauthorized',
  44. 402 => 'Payment Required',
  45. 403 => 'Forbidden',
  46. 404 => 'Not Found',
  47. 405 => 'Method Not Allowed',
  48. 406 => 'Not Acceptable',
  49. 407 => 'Proxy Authentication Required',
  50. 408 => 'Request Timeout',
  51. 409 => 'Conflict',
  52. 410 => 'Gone',
  53. 411 => 'Length Required',
  54. 412 => 'Precondition Failed',
  55. 413 => 'Request Entity Too Large',
  56. 414 => 'Request-URI Too Long',
  57. 415 => 'Unsupported Media Type',
  58. 416 => 'Requested Range Not Satisfiable',
  59. 417 => 'Expectation Failed',
  60. 429 => 'Too Many Requests', //still in draft but used for rate limiting
  61. 500 => 'Internal Server Error',
  62. 501 => 'Not Implemented',
  63. 502 => 'Bad Gateway',
  64. 503 => 'Service Unavailable',
  65. 504 => 'Gateway Timeout',
  66. 505 => 'HTTP Version Not Supported',
  67. 506 => 'No available spaces',
  68. 507 => 'Too many RentHistory',
  69. 508 => 'No RentHistory',
  70. 509 => 'DB error'
  71. );
  72. private $details;
  73. private $stage;
  74. /**
  75. * @param string $httpStatusCode http status code
  76. * @param string|null $errorMessage error message
  77. * @param array $details any extra detail about the exception
  78. * @param Exception $previous previous exception if any
  79. */
  80. public function __construct($httpStatusCode, $errorMessage = null, array $details = array(), Exception $previous = null)
  81. {
  82. $events = Scope::get('Restler')->getEvents();
  83. if(count($events)<= 1){
  84. $this->stage = 'setup';
  85. } else {
  86. $this->stage = $previous ? $events[count($events)-2] : end($events);
  87. }
  88. $this->details = $details;
  89. parent::__construct($errorMessage, $httpStatusCode, $previous);
  90. }
  91. /**
  92. * Get extra details about the exception
  93. *
  94. * @return array details array
  95. */
  96. public function getDetails()
  97. {
  98. return $this->details;
  99. }
  100. public function getStage()
  101. {
  102. return $this->stage;
  103. }
  104. public function getStages()
  105. {
  106. $e = Scope::get('Restler')->getEvents();
  107. $i = array_search($this->stage, $e);
  108. return array(
  109. 'success' => array_slice($e, 0, $i),
  110. 'failure' => array_slice($e, $i),
  111. );
  112. }
  113. public function getErrorMessage()
  114. {
  115. $statusCode = $this->getCode();
  116. $message = $this->getMessage();
  117. if (isset(RestException::$codes[$statusCode])) {
  118. $message = RestException::$codes[$statusCode] .
  119. (empty($message) ? '' : ': ' . $message);
  120. }
  121. return $message;
  122. }
  123. public function getSource()
  124. {
  125. $e = $this;
  126. while ($e->getPrevious()) {
  127. $e = $e->getPrevious();
  128. }
  129. return basename($e->getFile()) . ':'
  130. . $e->getLine() . ' at '
  131. . $this->getStage() . ' stage';
  132. }
  133. }