RestException.php 3.9 KB

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