63 lines
1.8KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. use yii\base\UserException;
  9. /**
  10. * HttpException represents an exception caused by an improper request of the end-user.
  11. *
  12. * HttpException can be differentiated via its [[statusCode]] property value which
  13. * keeps a standard HTTP status code (e.g. 404, 500). Error handlers may use this status code
  14. * to decide how to format the error page.
  15. *
  16. * Throwing an HttpException like in the following example will result in the 404 page to be displayed.
  17. *
  18. * ```php
  19. * if ($item === null) { // item does not exist
  20. * throw new \yii\web\HttpException(404, 'The requested Item could not be found.');
  21. * }
  22. * ```
  23. *
  24. * @author Qiang Xue <qiang.xue@gmail.com>
  25. * @since 2.0
  26. */
  27. class HttpException extends UserException
  28. {
  29. /**
  30. * @var integer HTTP status code, such as 403, 404, 500, etc.
  31. */
  32. public $statusCode;
  33. /**
  34. * Constructor.
  35. * @param integer $status HTTP status code, such as 404, 500, etc.
  36. * @param string $message error message
  37. * @param integer $code error code
  38. * @param \Exception $previous The previous exception used for the exception chaining.
  39. */
  40. public function __construct($status, $message = null, $code = 0, \Exception $previous = null)
  41. {
  42. $this->statusCode = $status;
  43. parent::__construct($message, $code, $previous);
  44. }
  45. /**
  46. * @return string the user-friendly name of this exception
  47. */
  48. public function getName()
  49. {
  50. if (isset(Response::$httpStatuses[$this->statusCode])) {
  51. return Response::$httpStatuses[$this->statusCode];
  52. } else {
  53. return 'Error';
  54. }
  55. }
  56. }