/** * I'm sure you sometimes want to show nice error to user! Well, if you call * Application::error(404), then it will end up here. This kind of errors are meant * only to show nice error to user. If you also want to log this or alert anyone that * this happened, then do that before calling Application::error() method. * * @param int $code The HTTP error code * @param string $message Optional message that will be visible to user * @param \Exception $e Optional exception, if any. Be careful, you might not * want to show the exceptions to users, but you would like to show it to developers? Then * use Application::inDevelopment() and inProduction() methods. */ public function error($code, $message = null, \Exception $e = null) { if (!headers_sent()) { switch ($code) { case 400: header('HTTP/1.0 400 Bad Request', true, 400); if ($message === null) { $message = 'Bad request'; } break; case 403: header('HTTP/1.0 403 Forbidden', true, 403); if ($message === null) { $message = 'Forbidden'; } break; case 404: header('HTTP/1.0 404 Not Found', true, 404); if ($message === null) { $message = 'Page Not Found'; } break; case 500: header('HTTP/1.0 500 Internal Server Error', true, 500); if ($message === null) { $message = 'Internal Server Error'; } break; case 503: header('HTTP/1.1 503 Service Temporarily Unavailable', true, 503); header('Status: 503 Service Temporarily Unavailable'); header('Retry-After: 300'); // 300 seconds / 5 minutes if ($message === null) { $message = 'Service Temporarily Unavailable'; } break; } } if ($message === null) { $message = 'Page Not Found'; } if ($this->isAjax()) { $data = array('success' => false, 'type' => 'http', 'code' => $code, 'message' => $message, 'exception' => $e !== null && Application::inDevelopment() ? $e->getMessage() : null); Json::create($data)->flush(); } else { $path = Application::getPublicPath("{$code}.php"); if (file_exists($path)) { $exception = $e; include $path; } else { // i don't know how to handle this message now!? throw new Exception($message === null ? 'Error ' . $code : $message); } } exit(0); }