javascript - Global Exception handling in angular js using promises : Exception handling: $q promises not catching Error code -


i have plan implemented err handling service globally existing application. not send error code server. have plan implement error code in angular using response error codes, need show custom bootstrap alert user.

implementation 1:

 loginservice.afterlogin(uname, encodeuricomponent(password))                        .then(function (response) {      }, function (response) {                           console.log(response + "response login");                       alert(response.message);      });    var loginservice = function ($http, $q, configapi) {       this.afterlogin = function (username, password) {         var result = $q.defer();          $http({             method: 'post',             url: // api call             headers: { 'content-type': 'application/json' }         })        .success(function (response) {             result.resolve(response);        })        .error(function (response) {            result.reject(response);                  });          return result.promise;     }   not catching error status.it shows whatever server errors comes.  have plan catch error code angular . 

enter image description here

implementation 2:

var configfunction = function ($stateprovider, $httpprovider, $urlrouterprovider, $locationprovider) {

var interceptor = function ($q, alertsmanager,$rootscope,$timeout) {      return {         request: function (config) {             console.log(config);             return config;         },          response: function (result) {             console.log('repos');             return result;        },         responseerror: function (rejection) {             if (rejection.status == 500)             {                 alert('bad request processed');                 $rootscope.showerror = true;                 $rootscope.error= 'error';                 $rootscope.errors= 'please contact administrators';                 $timeout(function () {                    $rootscope.dofade = true;                 }, 7500);               }             console.log(rejection.status);             return $q.reject(rejection);         }     } };    $httpprovider.interceptors.push(interceptor);     html part this: 

simple angular + bootstrap error messages

fake error


error: {{errormessage}}

problems: working properly. default alert bad request processed getting . bootstrap alert not displaying.

implementation:

i not specifying controller in login page. means there no controller in page.

so using rootscope display bootstrap alert. not displaying.

queries:

  • is there drawbacks using $httpprovider.
  • whats differencebetween $httpprovider , decorator global exception handling.

    $provide.decorator("$exceptionhandler", function($delegate, $injector){ return function(exception, cause){ var $rootscope = $injector.get("$rootscope"); $rootscope.adderror({message:"exception", reason:exception}); $delegate(exception, cause); }; });

so went scott allen method.

http://odetocode.com/blogs/scott/archive/2014/04/21/better-error-handling-in-angularjs.aspx

implementation 3:

specify $provide decoartor in config.

$provide.decorator("$exceptionhandler", function($delegate, $injector){         return function(exception, cause){             var $rootscope = $injector.get("$rootscope");             $rootscope.adderror({message:"exception", reason:exception});             $delegate(exception, cause);         };     });  cederightwebapp.factory("errors", function ($rootscope) {     debugger;     return {         catch: function (message) {             return function (reason) {                 $rootscope.adderror({ message: message, reason: reason })              };         }     }; });     loginservice.afterlogin(uname, encodeuricomponent(password))                        .then(function (response) {      }, function (response) {                           console.log(response + "response login");                       alert(response.message);      }).catch(errors.catch("could not complete work!")); 

getting error:

 $rootscope.adderror not function. 


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -