基于youzan小程序sentry SDK改造

封装wx.request

// 保存原来的
request(options) {
   let oldRequestSuccess = options.success
    let oldRequestFail = options.fail

    options.success = function (res) {
        Raven.captureBreadcrumb({
            category: 'ajax success',
            data: {
                method: options.method || 'get',
                url: options.url,
                status_code: res.statusCode
            }
        })
        if(oldRequestSuccess){
            oldRequestSuccess(res)
        }
    }

    options.fail = function (res) {
        Raven.captureBreadcrumb({
            category: 'ajax fail',
            data: {
                method: options.method || 'get',
                url: options.url,
                errMsg: res.errMsg
            }
        })
        if(oldRequestFail) {
            oldRequestFail(res)
        }
    }
    wx.request(options)
},

优化console上传的数据

原来console在上传对象时,对象只能显示成[Object Object]
Object进行JSON编码,以更好地显示信息

73: var msg = safeJoin(args, ' ');

function isArray(what) {
    return Object.prototype.toString.call(what) === '[object Array]';
}
/**
 * Join values in array
 * @param input array of values to be joined together
 * @param delimiter string to be placed in-between values
 * @returns {string}
 */
function safeJoin(input, delimiter) {
    if (!isArray(input)) return '';

    var output = [];

    for (var i = 0; i < input.length; i++) {
        try {
            if (typeof input[i] === 'object') {
                // 如果是对象,则进行JSON编码
                output.push(JSON.stringify(input[i], null, 2));
            } else {
                output.push(String(input[i]));
            }

        } catch (e) {
            output.push('[value cannot be serialized]');
        }
    }

    return output.join(delimiter);
}

你可能感兴趣的:(基于youzan小程序sentry SDK改造)