Commit fb64ec5a by Scott

Add helper functions for HTTP errors

parent af4c8fbb
......@@ -57,7 +57,7 @@ if (count($questions) > 0) {
$qa_content['title'] = qa_lang_html_sub('main/questions_tagged_x', qa_html($tag));
} else {
$qa_content['title'] = qa_lang_html('main/no_questions_found');
header('HTTP/1.0 404 Not Found');
qa_404();
}
if (isset($userid) && isset($tagword)) {
......
......@@ -1313,6 +1313,41 @@ function convert_to_bytes($unit, $value)
/**
* Issue a HTTP status code header.
* @param int $code
* @param string $message
* @return void
*/
function qa_http_error($code, $message)
{
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
$code = (int) $code;
header("$protocol $code $message");
}
/**
* Issue a HTTP 404 header.
* @return void
*/
function qa_404()
{
qa_http_error('404', 'Not Found');
}
/**
* Issue a HTTP 500 header.
* @return void
*/
function qa_500()
{
qa_http_error('500', 'Internal Server Error');
}
/**
* Return true if we are responding to an HTTP GET request
* @return bool True if the request is GET
*/
......
......@@ -26,7 +26,7 @@
function qa_blob_db_fail_handler()
{
header('HTTP/1.1 500 Internal Server Error');
qa_500();
qa_exit('error');
}
......@@ -89,7 +89,7 @@ if (isset($blob) && isset($blob['content'])) {
echo $blob['content'];
} else {
header('HTTP/1.0 404 Not Found');
qa_404();
}
$qa_db->disconnect();
......@@ -42,7 +42,7 @@ require_once QA_INCLUDE_DIR . 'app/options.php';
*/
function qa_feed_db_fail_handler($type, $errno = null, $error = null, $query = null)
{
header('HTTP/1.1 500 Internal Server Error');
qa_500();
echo qa_lang_html('main/general_error');
qa_exit('error');
}
......@@ -53,7 +53,7 @@ function qa_feed_db_fail_handler($type, $errno = null, $error = null, $query = n
*/
function qa_feed_not_found()
{
header('HTTP/1.0 404 Not Found');
qa_404();
echo qa_lang_html('misc/feed_not_found');
qa_exit();
}
......
......@@ -26,7 +26,7 @@
function qa_image_db_fail_handler()
{
header('HTTP/1.1 500 Internal Server Error');
qa_500();
qa_exit('error');
}
......
......@@ -27,7 +27,7 @@ if (!defined('QA_VERSION')) { // don't allow this page to be requested directly
require_once QA_INCLUDE_DIR . 'app/format.php';
header('HTTP/1.0 404 Not Found');
qa_404();
qa_set_template('not-found');
......
......@@ -51,7 +51,7 @@ class ExceptionHandler
private function handlePageNotFoundException(PageNotFoundException $exception)
{
header('HTTP/1.1 404 Not Found');
qa_404();
$qa_content = $this->handleErrorMessageException($exception);
$qa_content['suggest_next'] = qa_html_suggest_qs_tags(qa_using_tags());
......@@ -61,7 +61,7 @@ class ExceptionHandler
private function handleMethodNotAllowedException(MethodNotAllowedException $exception)
{
header('HTTP/1.1 405 Method Not Allowed');
qa_http_error('405', 'Method Not Allowed');
$qa_content = $this->handleErrorMessageException($exception);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment