You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.8 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage app
  7. * @since 2010-02-25
  8. */
  9. class ErrorAction extends Action
  10. {
  11. /**
  12. * @var ErrorException|ErrorHTTPException
  13. */
  14. public $exception;
  15. protected $ajax_error = false;
  16. public function __construct($exception)
  17. {
  18. $this->exception = $exception;
  19. parent::__construct();
  20. }
  21. protected function execute()
  22. {
  23. if ($this->exception instanceof Error404Exception) {
  24. $this->template = 404;
  25. } elseif ($this->exception instanceof ErrorHTTPException) {
  26. $this->template = 'HTTP';
  27. } else {
  28. $this->template = 500;
  29. }
  30. $this->logError();
  31. $this->sendHTTPCode();
  32. }
  33. public function fetch()
  34. {
  35. if ($this->isAjaxActionError()) {
  36. return $this->exception->getMessage();
  37. }
  38. return parent::fetch();
  39. }
  40. protected function getTemplate()
  41. {
  42. return '/actions/' . $this->template;
  43. }
  44. protected function sendHttpCode()
  45. {
  46. if ($this->exception instanceof ErrorHTTPException) {
  47. header($this->exception->getHTTPHeader());
  48. } else {
  49. header('HTTP/1.0 500 Internal Server Error');
  50. }
  51. }
  52. protected function logError()
  53. {
  54. if (!$this->exception instanceof Error404Exception) {
  55. ErrorHandler::logError($this->exception);
  56. }
  57. }
  58. /**
  59. * Check, if exception was thrown from AjaxAction Class
  60. * @return bool
  61. */
  62. protected function isAjaxActionError()
  63. {
  64. return $this->ajax_error;
  65. }
  66. /**
  67. * Set if exception was thrown from AjaxAction subclass
  68. */
  69. public function setAjaxError()
  70. {
  71. $this->ajax_error = true;
  72. }
  73. }