From 8728f624789df4977e279f6f575f10011d944a0b Mon Sep 17 00:00:00 2001 From: root Date: Mon, 20 Oct 2014 17:39:41 -0400 Subject: [PATCH] Add exception handler for entire API. --- api/api.php | 16 ++++++++-------- js/controllers/login.js | 7 +++---- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/api/api.php b/api/api.php index 65cc2dd..5a3a27b 100644 --- a/api/api.php +++ b/api/api.php @@ -16,15 +16,15 @@ $jsonResponse = new JsonResponse(); require_once('helpers.php'); // Must come after $jsonResponse exists. // Catch Exception if connection to DB failed -try { - R::setup('sqlite:taskboard.db'); - createInitialUser(); -} catch(Exception $e) { - $app->response->setStatus(503); - $jsonResponse->message = 'Connection to database failed. Ensure api is writable.'; +function exceptionHandler($exception) { + header('Content-Type: application/json'); + http_response_code(503); + echo '{"message": "API Error."}'; +}; +set_exception_handler('exceptionHandler'); - $app->response->setBody($jsonResponse->asJson()); -} +R::setup('sqlite:taskboard.db'); +createInitialUser(); $app->notFound(function() use ($app, $jsonResponse) { $app->response->setStatus(404); diff --git a/js/controllers/login.js b/js/controllers/login.js index 81d88ba..2f2510c 100644 --- a/js/controllers/login.js +++ b/js/controllers/login.js @@ -28,10 +28,9 @@ function ($rootScope, $scope, $location, $window, UserService, AuthenticationSer $location.path('/boards'); }).error(function(data, status) { $scope.isSaving = false; - if (status === 401) { - $scope.errors.push(data.message); - } else { - $scope.errors.push('Something went wrong. Please try again.'); + $scope.errors.push(data.message); + if (status === 503) { + $scope.errors[0] = $scope.errors[0] + ' Ensure api directory is writable.'; } }); };