107 lines
2.5 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 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. $error = 0;
  58. $ex = $this->exception;
  59. if ($ex instanceof ErrorException) {
  60. $error = $ex->getSeverity();
  61. }
  62. switch ($error) {
  63. case E_NOTICE:
  64. $error = 'Notice';
  65. break;
  66. case E_WARNING:
  67. $error = 'Warning';
  68. break;
  69. case E_ERROR:
  70. $error = 'Fatal Error';
  71. break;
  72. default:
  73. $error = 'Unknown Error';
  74. break;
  75. }
  76. $message = 'PHP ' . $error . ': ' . $ex->getMessage() . ' in ' . $ex->getFile()
  77. . ' on line ' . $ex->getLine();
  78. error_log($message);
  79. }
  80. }
  81. /**
  82. * Check, if exception was thrown from AjaxAction Class
  83. * @return bool
  84. */
  85. protected function isAjaxActionError()
  86. {
  87. return $this->ajax_error;
  88. }
  89. /**
  90. * Set if exception was thrown from AjaxAction subclass
  91. */
  92. public function setAjaxError()
  93. {
  94. $this->ajax_error = true;
  95. }
  96. }