RestException.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. );
  69. private $details;
  70. private $stage;
  71. /**
  72. * @param string $httpStatusCode http status code
  73. * @param string|null $errorMessage error message
  74. * @param array $details any extra detail about the exception
  75. * @param Exception $previous previous exception if any
  76. */
  77. public function __construct($httpStatusCode, $errorMessage = null, array $details = array(), Exception $previous = null)
  78. {
  79. $events = Scope::get('Restler')->getEvents();
  80. if(count($events)<= 1){
  81. $this->stage = 'setup';
  82. } else {
  83. $this->stage = $previous ? $events[count($events)-2] : end($events);
  84. }
  85. $this->details = $details;
  86. parent::__construct($errorMessage, $httpStatusCode, $previous);
  87. }
  88. /**
  89. * Get extra details about the exception
  90. *
  91. * @return array details array
  92. */
  93. public function getDetails()
  94. {
  95. return $this->details;
  96. }
  97. public function getStage()
  98. {
  99. return $this->stage;
  100. }
  101. public function getStages()
  102. {
  103. $e = Scope::get('Restler')->getEvents();
  104. $i = array_search($this->stage, $e);
  105. return array(
  106. 'success' => array_slice($e, 0, $i),
  107. 'failure' => array_slice($e, $i),
  108. );
  109. }
  110. public function getErrorMessage()
  111. {
  112. $statusCode = $this->getCode();
  113. $message = $this->getMessage();
  114. if (isset(RestException::$codes[$statusCode])) {
  115. $message = RestException::$codes[$statusCode] .
  116. (empty($message) ? '' : ': ' . $message);
  117. }
  118. return $message;
  119. }
  120. public function getSource()
  121. {
  122. $e = $this;
  123. while ($e->getPrevious()) {
  124. $e = $e->getPrevious();
  125. }
  126. return basename($e->getFile()) . ':'
  127. . $e->getLine() . ' at '
  128. . $this->getStage() . ' stage';
  129. }
  130. }