如何为所有catch块编写全局函数,每个catch块可以将自己的自定义消息作为参数传递?

时间:2017-05-21 04:50:04

标签: javascript angularjs error-handling closures hoisting

userComponent.js

 function users(userService, helperService) {
        return userService.get()
       .then(function(){})
       .catch(function (errors) {
             if (angular.isArray(errors)) {
        for (var i = errors.length - 1; i > -1; i--) {
            console.log(errors[i]);
        }
    }
    window.alert('unable to get data');
        });
    }

userService.js

 function get() {
    return dataService.get("users").then(function (data) {
        if (....) {
            return data
        }
        else {
            throw new Error('error in userService')
        }
    }).catch(helperService.ErrorHandler('error in userService'));
}

dataService.js

function get(endpoint, data) {
        return $http.get(API_URL + endpoint).then(function (data) {
            if (status check) {
                return data;
            }
            else {
                throw new Error('error in dataService')
            }
        }).catch(helperService.ErrorHandler('error in dataService'));
    }

helperService

function ErrorHandler (error) {

};

我需要为所有catch块编写单个ErrorHandler函数并将数组返回给userComponent.js ..(使用javascript闭包和提升属性..)

0 个答案:

没有答案