Saturday, March 1, 2014

Javascript Interceptor or aspect-oriented programming Pattern - in progress

User case:

User login mobile application, and on behave of user mobile application sends every Ajax request to back-end service along with an authorization token, server will give 401 status if "session" or token is expired time out.

Solution:

a. Use global .Ajaxsetting() to specify the status code 401 call back function. It is probably the easiest way. However in this approach the ajax call error will still bubble up event chain and will still trigger the subsequently error handler. So it only works if you redirect user to login page immediately.

b. Use promise chain error call back to trap the error.

Let's look at promise.then(success, error) function. It returns a promise itself.
The promise is resolved if 1) success/error return a value or 2) the returned value is a promise, then it is resolved with this promise.
The promise is rejected if success / error throws an error.

So if we simply return a dummy value when 401 is caught, then it will be passed to the next successHandler. then in the successHandler we need to differentiate the trapped error and real success response.

No comments:

Post a Comment