Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS Handle different error codes in different places
    primarykey
    data
    text
    <p>I have a service that contains a generic call to $http that all ajax calls uses. Here I have "centralized" error handling where the status codes results in redirects. </p> <p>"FieldsSync" service:</p> <pre><code>return $http({ method: 'POST', url: url, data: $.extend(defaultPostData, postData) }).error(function(data, status) { switch (status) { case 401: // "Unauthorized". Not logged in. redirect.toLoginPage(); break; case 403: // "Forbidden". Role has changed. redirect.toLogoutPage(); break; } }); </code></pre> <p>When calling one of the service functions from the controller I always return the deferred object to be able to hook up more error callbacks to be able to handle errors that should result in some kind of feedback for the user.</p> <p>Controller:</p> <pre><code>fieldsSync.createField(newField).success(function(data) { ... }).error(function(data, status) { switch (status) { // &lt;--- DO NOT WANT case 401: case 403: return; // These errors are handled (=redirects) in the generic error callback and we don't want to show do anything while it redirects. } ... // "Individual" error handling. =Displaying messages and stuff }); </code></pre> <p>But because I don't want error messages popping up before the redirect occurs I have to exit the error callback if the status codes already have been handled.</p> <p>The question is: How do i get rid of the switch cases in my controllers? Is it possible to exit the chain of error callbacks when a specific error code has been handled? Or is it possible to work around this in a less ugly way? :)</p> <p>This is a reoccurring problem for me, and my mind seems stuck.</p> <p>I have checked the docs and cant find any pretty solution for this in either $http or $q.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload