Commit fb64ec5a by Scott

Add helper functions for HTTP errors

parent af4c8fbb
...@@ -57,7 +57,7 @@ if (count($questions) > 0) { ...@@ -57,7 +57,7 @@ if (count($questions) > 0) {
$qa_content['title'] = qa_lang_html_sub('main/questions_tagged_x', qa_html($tag)); $qa_content['title'] = qa_lang_html_sub('main/questions_tagged_x', qa_html($tag));
} else { } else {
$qa_content['title'] = qa_lang_html('main/no_questions_found'); $qa_content['title'] = qa_lang_html('main/no_questions_found');
header('HTTP/1.0 404 Not Found'); qa_404();
} }
if (isset($userid) && isset($tagword)) { if (isset($userid) && isset($tagword)) {
......
...@@ -1313,6 +1313,41 @@ function convert_to_bytes($unit, $value) ...@@ -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 true if we are responding to an HTTP GET request
* @return bool True if the request is GET * @return bool True if the request is GET
*/ */
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
function qa_blob_db_fail_handler() function qa_blob_db_fail_handler()
{ {
header('HTTP/1.1 500 Internal Server Error'); qa_500();
qa_exit('error'); qa_exit('error');
} }
...@@ -89,7 +89,7 @@ if (isset($blob) && isset($blob['content'])) { ...@@ -89,7 +89,7 @@ if (isset($blob) && isset($blob['content'])) {
echo $blob['content']; echo $blob['content'];
} else { } else {
header('HTTP/1.0 404 Not Found'); qa_404();
} }
$qa_db->disconnect(); $qa_db->disconnect();
...@@ -42,7 +42,7 @@ require_once QA_INCLUDE_DIR . 'app/options.php'; ...@@ -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) 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'); echo qa_lang_html('main/general_error');
qa_exit('error'); qa_exit('error');
} }
...@@ -53,7 +53,7 @@ function qa_feed_db_fail_handler($type, $errno = null, $error = null, $query = n ...@@ -53,7 +53,7 @@ function qa_feed_db_fail_handler($type, $errno = null, $error = null, $query = n
*/ */
function qa_feed_not_found() function qa_feed_not_found()
{ {
header('HTTP/1.0 404 Not Found'); qa_404();
echo qa_lang_html('misc/feed_not_found'); echo qa_lang_html('misc/feed_not_found');
qa_exit(); qa_exit();
} }
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
function qa_image_db_fail_handler() function qa_image_db_fail_handler()
{ {
header('HTTP/1.1 500 Internal Server Error'); qa_500();
qa_exit('error'); qa_exit('error');
} }
......
...@@ -27,7 +27,7 @@ if (!defined('QA_VERSION')) { // don't allow this page to be requested directly ...@@ -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'; require_once QA_INCLUDE_DIR . 'app/format.php';
header('HTTP/1.0 404 Not Found'); qa_404();
qa_set_template('not-found'); qa_set_template('not-found');
......
...@@ -51,7 +51,7 @@ class ExceptionHandler ...@@ -51,7 +51,7 @@ class ExceptionHandler
private function handlePageNotFoundException(PageNotFoundException $exception) private function handlePageNotFoundException(PageNotFoundException $exception)
{ {
header('HTTP/1.1 404 Not Found'); qa_404();
$qa_content = $this->handleErrorMessageException($exception); $qa_content = $this->handleErrorMessageException($exception);
$qa_content['suggest_next'] = qa_html_suggest_qs_tags(qa_using_tags()); $qa_content['suggest_next'] = qa_html_suggest_qs_tags(qa_using_tags());
...@@ -61,7 +61,7 @@ class ExceptionHandler ...@@ -61,7 +61,7 @@ class ExceptionHandler
private function handleMethodNotAllowedException(MethodNotAllowedException $exception) 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); $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