Add exception handler for entire API.
This commit is contained in:
parent
d1d4eac986
commit
8728f62478
16
api/api.php
16
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);
|
||||
|
@ -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.';
|
||||
}
|
||||
});
|
||||
};
|
||||
|
Reference in New Issue
Block a user