I recently read an article about network exceptions that 99% of programs do not take into account.

Most programs only consider scenarios where the interface works, and all kinds of exceptions that users encounter when using our product are thrown in a try catch that looks ok. If we do not do a good job of abnormal compatibility and bottom handling, it will greatly affect the user experience, serious will also bring security and capital loss risk.

Therefore, the author analyzed some open source wechat applets on GitHub and found that most of the code exception handling is really insufficient.

  • The login interface only considers the success case, not the failure case
// Invoke the login interface
wx.login({
    success: function() {
        wx.getUserInfo({
            success: function(res) {
                that.globalData.userInfo = res.userInfo;
                typeof cb == "function"&& cb(that.globalData.userInfo); }}); }});Copy the code
  • Network requests consider only then and not catch

    util.getData(index_api).then(function(data) {
        //this.setData({
        //
        / /});
        console.log(data);
    });
    Copy the code
  • Consider exceptions but do not handle them properly

    db.collection("config")
        .where({})
        .get()
        .then(res= > {
            console.log(res);
            if (res.data.length > 0) {
                Taro.setStorage({
                    key: "config_gitter".data: res.data[0]}); } }) .catch(err= > {
            console.error(err);
        });
    Copy the code

Maybe 99 percent of the time the interface will return normally and only 1 percent of the time it will fail. This may not seem like a big deal, but considering the size of the user base, it’s not. Assuming there are 1 million users, 10,000 users are experiencing an exception, and if the user is using it very frequently, more than 10,000 users are affected. In addition, today’s products are all about experience first, if you encounter such problems, users are most likely to abandon you, losing customers is equal to losing income.

How to properly handle interface exceptions is a serious matter that should be taken seriously.

Handle request exceptions properly

So what should be done? First of all, the processing code of the request exception should be defined. For example, parameters of wechat open interface include FAIL (” failed callback function invoked by the interface “) and catch part of Promise. Second, according to the possible consequences of the exception, do the corresponding processing in the function. If it causes subsequent operations to fail, or if there is no feedback from the interface, it should be handled correctly in the FAIL callback. If you really think there’s little chance of a problem, at least write an exception report. You know what’s going on when something goes wrong.

Below is the parameter list of wechat Pay interface, which includes the callback function (FAIL) that the interface calls.

And there are official examples:

wx.requestPayment({
    timeStamp: "".nonceStr: "".package: "".signType: "MD5".paySign: "",
    success(res) {},
    fail(res) {}
});
Copy the code

In the callback functionfailAbnormal reporting in medium

To ensure that we fully understand the health of the applet, we report exceptions. The micro program plug-in of Fundebug can not only automatically catch exceptions, but also actively report exceptions through the API interface.

According to its official documentation:

With fundebug.notify(), you can send custom error messages to Fundebug

Name: indicates the error name. The parameter type is a string

“Message” : indicates an error message. The parameter type is a string

Option: Optional object of type object, used to send some additional information

Example:

fundebug.notify("Test"."Hello, Fundebug!", {
 metaData: {
     company: "Cloud jiaqi".location: "Xiamen"}});Copy the code

First create a small program monitor project in Fundebug and plug in the plug-in as instructed, then test it by calling wx.requestPayment under the onLaunch function of app.js.

Fundebug’s wechat applet plug-in caught and reported an exception:

The metaData TAB also shows the metaData we configured, which is the RES parameter of the fail callback function.

Therefore, we can know that the failure is due to overdue orders.

In addition, if you stay too long on the TWO-DIMENSIONAL code page, it will trigger an error:

By simply adding a few lines of code, you can become familiar with the exceptions of a small program. Fundebug can also monitor JavaScript execution errors, automatically catch Wx. request errors, and monitor slow HTTP requests.

About Fundebug

Fundebug focuses on real-time BUG monitoring for JavaScript, wechat applets, wechat games, Alipay applets, React Native, Node.js and Java online applications. Since its official launch on November 11, 2016, Fundebug has handled over 1 billion error events in total, and paid customers include Sunshine Insurance, Walnut Programming, Lychee FM, Zhangmen 1-to-1, Weimai, Qingtuanshe and many other brand enterprises. Welcome to try it for free!

Copyright statement

Reprint please indicate the author Fundebug and this article addresses: blog.fundebug.com/2019/07/08/…