0

I having written an error handling function that displays a Modal when some error(Catch) occurs. The following is its implementation:

function displayErrorModal(error){
    $scope.error = error;
    $scope.modalInstance = $uibModal.open({
        templateUrl: '/popeye-application/components/dashboard/dashboard-error-handler.template.jsp',
        size: 'lg',
        scope: $scope,
        backdrop: 'static'
    });
}

Whenever there is an error, I call this an handle the error. Following is one such scenario:

function getProductName (name) {
    Service.getProductNameByProductID(
        filter(name)
    ).then(function (response) {
        // Happy case
    }).catch(function (error) {
        displayErrorModal(error);
    });
}

However, I am not able to handle any 401 Error(Session Expired) using this. How can I handle 401 Errors in AngularJS? Thanks in advance.

Dext1232
  • 241
  • 4
  • 18
  • Have you tried wrapping displayErrorModal inside a timeout callback? Inject `$timeout` into your controller and change content of the catch-block to `$timeout(() => displayErrorModal(error))` – andMarkus Dec 21 '20 at 10:24
  • No, I haven't tried that one, can you tell more on how do I do it? – Dext1232 Dec 21 '20 at 10:48
  • You can handle 401 errors globally be implementing $http interceptor like https://stackoverflow.com/a/23720035/1443361 – Matija Sestak Dec 27 '20 at 18:09

0 Answers0