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.

85 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. $this->template = 500;
  24. if ($this->exception instanceof Error404Exception) {
  25. $this->template = 404;
  26. } elseif ($this->exception instanceof ErrorHTTPException) {
  27. $this->template = 'HTTP';
  28. }
  29. $this->logError();
  30. $this->sendHTTPCode();
  31. }
  32. public function fetch()
  33. {
  34. if ($this->isAjaxActionError()) {
  35. return $this->exception->getMessage();
  36. }
  37. return parent::fetch();
  38. }
  39. protected function getTemplate()
  40. {
  41. return '/actions/' . $this->template;
  42. }
  43. protected function sendHttpCode()
  44. {
  45. switch ($this->template) {
  46. case 404:
  47. case 'HTTP':
  48. header($this->exception->getHTTPHeader());
  49. break;
  50. default:
  51. header('HTTP/1.0 500 Internal Server Error');
  52. }
  53. }
  54. protected function logError()
  55. {
  56. if ($this->template == 500) {
  57. ErrorHandler::logError($this->exception);
  58. }
  59. }
  60. /**
  61. * Check, if exception was thrown from AjaxAction Class
  62. * @return bool
  63. */
  64. protected function isAjaxActionError()
  65. {
  66. return $this->ajax_error;
  67. }
  68. /**
  69. * Set if exception was thrown from AjaxAction subclass
  70. */
  71. public function setAjaxError()
  72. {
  73. $this->ajax_error = true;
  74. }
  75. }