Commit 4d1217e4 authored by thiaramus's avatar thiaramus
Browse files

Added HTTP response codes

parent ac2678d5
......@@ -58,7 +58,7 @@
public function run() {
if (!array_key_exists(1, $this->apiCallPath)) {
$this->syntaxErrorMessage('Nodelist function is undefined');
$this->syntaxErrorMessage('Nodelist function is undefined', 404);
}
if ($this->apiCallPath[1] === 'namesearch') {
......@@ -68,7 +68,7 @@
$aka = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_STRING);
$this->response($this->akaSearch($aka));
} else {
$this->syntaxErrorMessage('Unknown Nodelist function');
$this->syntaxErrorMessage('Unknown Nodelist function', 404);
}
}
}
\ No newline at end of file
......@@ -57,7 +57,8 @@
*
* @param $response
*/
protected function response($response) {
protected function response($response, $responseCode = 200) {
http_response_code($responseCode);
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, PATCH, OPTIONS');
header('Access-Control-Max-Age: 1000');
......@@ -65,9 +66,8 @@
echo json_encode($response, JSON_PRETTY_PRINT);
}
protected function syntaxErrorMessage($message = 'Syntax error') {
http_response_code(400);
$this->response([$message]);
protected function syntaxErrorMessage($message = 'Syntax error', $responseCode = 400) {
$this->response([$message], $responseCode);
die();
}
......
......@@ -53,13 +53,13 @@
if (is_a($apiClass, 'mainApiParent')) {
$apiClass->run();
} else {
throw new Exception('Invalid API endpoint');
throw new Exception('Invalid API endpoint', 404);
}
} else {
throw new Exception('Unknown API endpoint');
throw new Exception('Unknown API endpoint', 404);
}
} catch (Exception $e) {
http_response_code(400);
http_response_code($e->getCode() ? $e->getCode() : 400);
echo $e->getMessage();
}
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