Api exeption handler

This commit is contained in:
xinatorus 2020-04-25 11:38:06 +02:00
parent 8300e47b76
commit 119149bf5e
2 changed files with 11 additions and 8 deletions

View File

@ -34,12 +34,15 @@ spl_autoload_register("Autoloader::ClassLoader");
Autoloader::setRoot('/var/www/dev.steelants.cz/vasek/home-update/'); Autoloader::setRoot('/var/www/dev.steelants.cz/vasek/home-update/');
class ErrorHandler { class ErrorHandler {
static function exception($exception){ static function exception($exception){
error_log($exception); error_log($exception);
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, $exception->getCode()); http_response_code($exception->getCode());
echo '<h1>Oops!</h1><p>Something went wrong!</p>'; $message = [
exit; 'code' => $exception->getCode(),
} 'message' => $exception->getMessage(),
];
echo json_encode($message);
}
} }
set_exception_handler("ErrorHandler::exception"); set_exception_handler("ErrorHandler::exception");

View File

@ -23,10 +23,10 @@ class ApiController {
$authManager = new AuthManager(); $authManager = new AuthManager();
$this->authenticated = $authManager>validateToken($this->headers['HTTP_AUTHORIZATION']); $this->authenticated = $authManager>validateToken($this->headers['HTTP_AUTHORIZATION']);
if(!$this->authenticated){ if(!$this->authenticated){
throw new Exception("Auth required", 401); throw new Exception("Authorization required", 401);
} }
} else { } else {
throw new Exception("Auth required", 401); throw new Exception("Authorization required", 401);
} }
} }