Skip to content

Angular打印错误的minErr函数 #18

@Wscats

Description

@Wscats

angular minErr函数的源码是这样的,这个函数主要用来输出错误的提示信息

function minErr(module) {
    return function() {
        var message, i, code = arguments[0],
            prefix = "[" + (module ? module + ":" : "") + code + "] ",
            template = arguments[1],
            templateArgs = arguments,
            stringify = function(obj) {
                return "function" == typeof obj ? obj.toString().replace(/ \{[\s\S]*$/, "") : "undefined" == typeof obj ? "undefined" : "string" != typeof obj ? JSON.stringify(obj) : obj
            };
        for (message = prefix + template.replace(/\{\d+\}/g, function(match) {
                var arg, index = +match.slice(1, -1);
                return index + 2 < templateArgs.length ? (arg = templateArgs[index + 2], "function" == typeof arg ? arg.toString().replace(/ ?\{[\s\S]*$/, "") : "undefined" == typeof arg ? "undefined" : "string" != typeof arg ? toJson(arg) : arg) : match
            }), message = message + "\nhttp://errors.angularjs.org/1.2.1/" + (module ? module + "/" : "") + code, i = 2; i < arguments.length; i++) message = message + (2 == i ? "?" : "&") + "p" + (i - 2) + "=" + encodeURIComponent(stringify(arguments[i]));
        return new Error(message)
    }
}

从这个函数中可以学到几点东西
首先是里面这个正则
template.replace(/\{\d+\}/g)
这个正则是匹配所有"{1个或更多数字}"形式的字符串
例如

var a = "{1}233{2}";
message = a.replace(/(\{)(\d+)(\})/g, 'wscats');//加了括号方便理解

相当于
new RegExp("\{\d+\}", "g")//g代表 global match(全定匹配)

其次是return new Error(message),这个

ngMinErr = minErr("ng");
throw ngMinErr("badname","hasOwnProperty is not a valid {0} name","module");

抛出打印的异常

function checkInput(x) {
    try {
        if (isNaN(parseInt(x))) {
            throw new Error("Input is not a number.");
        }
    } catch (e) {
        document.write(e.description);
        console.log(new Error("Input is not a number."));
    }
}
checkInput("not a number");

输出如下的异常信息
Uncaught Error: [ng:badname] hasOwnProperty is not a valid module name
http://errors.angularjs.org/1.2.1/ng/badname?p0=module"

这里如果throw new Error("wscats");之后,后面的代码就会得不到执行

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions