From 119149bf5efcc002301f6a4c965c890cb2d0a991 Mon Sep 17 00:00:00 2001 From: xinatorus Date: Sat, 25 Apr 2020 11:38:06 +0200 Subject: [PATCH] Api exeption handler --- app/Bootstrap.php | 15 +++++++++------ library/ApiController.php | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/Bootstrap.php b/app/Bootstrap.php index da9745b..67a9395 100644 --- a/app/Bootstrap.php +++ b/app/Bootstrap.php @@ -34,12 +34,15 @@ spl_autoload_register("Autoloader::ClassLoader"); Autoloader::setRoot('/var/www/dev.steelants.cz/vasek/home-update/'); class ErrorHandler { - static function exception($exception){ - error_log($exception); - header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, $exception->getCode()); - echo '

Oops!

Something went wrong!

'; - exit; - } + static function exception($exception){ + error_log($exception); + http_response_code($exception->getCode()); + $message = [ + 'code' => $exception->getCode(), + 'message' => $exception->getMessage(), + ]; + echo json_encode($message); + } } set_exception_handler("ErrorHandler::exception"); diff --git a/library/ApiController.php b/library/ApiController.php index c4242d6..b3cc55b 100644 --- a/library/ApiController.php +++ b/library/ApiController.php @@ -23,10 +23,10 @@ class ApiController { $authManager = new AuthManager(); $this->authenticated = $authManager>validateToken($this->headers['HTTP_AUTHORIZATION']); if(!$this->authenticated){ - throw new Exception("Auth required", 401); + throw new Exception("Authorization required", 401); } } else { - throw new Exception("Auth required", 401); + throw new Exception("Authorization required", 401); } }