Project sharing nine: Exception handling for clients

Source: Internet
Author: User

First, the handling of abnormal

Exception handling is a feature that all programs should have, especially network exceptions, which are more common in apps. Let's take a look at how the Chitu Store handles exceptions. There are two main types of exceptions:

1, the service side throws the exception, namely the service side returns the error message, when the service side exception, returns the error JSON information:

{     ' errorobject ',     Code:' xxxx ' }

2, the client appears the exception, the client's exception, the vast number of network anomalies, Chitu Store mainly deal with this kind of exception.

Now we need to see how the Chitu Store is handled.

Second, rewrite the Ajax function

The error message returned by the server is captured by overriding the Ajax function. is the if (data. Type = = ' Errorobject ') This sentence, if it is an error message, will cause an error event, namely: Services.error.fire (data, Textstatus, JQXHR),

All of the listening Services.error events are receiving this error.

    var_ajax =$.ajax; $.extend ($, {ajax:function(options) {Options.data= Options.data | | {}; varresult = $.            Deferred (); _ajax (Options). Done ($.proxy (function(data, Textstatus, JQXHR) {if(Data. Type = = ' Errorobject ') {                    if(Data. Code = = ' Success ') {                         This. _result.resolve (data, Textstatus, JQXHR); return; }                    if($.isfunction ( This. _result.element)) {data.element= This. _result.element ();                    } services.error.fire (data, Textstatus, JQXHR);  This. _result.reject (data, Textstatus, JQXHR); return; }                 This. _result.resolve (data, Textstatus, JQXHR); }, {_result:result}). Fail ($.proxy (function(JQXHR, textstatus) {varErr ={code:textstatus, status:jqXHR.status, Message:jqXHR.statusText}; if($.isfunction ( This. _result.element)) {err[' element '] = This. _result.element ();                    } services.error.fire (ERR);  This. _result.reject (ERR);            }, {_result:result}); returnresult; }    });
Third, monitoring and processing errors

Locate the Errorhandler.ts file, all the error messages are processed here. Services.error.add is used to monitor error events, you can see, if it is not logged in, it will jump to the login page, let the user log in, or call the ShowError function, to display the error.

Services.error.add (function  (error) {    if (Error). Code = = ' Notlogin ' | | Error. Code = = ' tokenrequired ')        {var return_url = ';         if (Location.hash.length > 1)             = Location.hash.substr (1);         return app.showpage (' User_login ', {redirecturl:return_url});    };    ShowError (Error);});

Write more simple, have any questions can give me a message.

Project sharing eight: Pop-up window based on button click event

Project sharing Seven: Clients prevent forms from repeating commits

Project sharing six: delayed loading of pictures

Project sharing five: H5 image compression and upload

Project sharing four: Updating the shopping cart page

Project sharing three: passing values between pages

Project Sharing II: The processing of numbers in the APP's Little Red dots

Project sharing one: Using Iscroll in the project the pits they met

Project sharing nine: Exception handling for clients

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.