本文介绍了捕获服务器端的所有JavaScript客户端错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何捕获客户端代码中发生的任何异常,例如Chrome开发人员工具上的暂停捕获的异常?

How can I catch any exception that occurs in the client side code like "Pause On Caught Exceptions" on chrome developer tools?

推荐答案

我找到了解决方案!

我使用过C#和MVC。

I have used the C# and MVC.

添加一个新类来自定义你的js文件包,如下所示:

Add a new class to customize your js files bundle like this:

public class CustomScriptBundle : ScriptBundle
{
    public CustomScriptBundle(string virtualPath) : base(virtualPath)
    {
        Builder = new CustomScriptBundleBuilder();
    }

    public CustomScriptBundle(string virtualPath, string cdnPath)
        : base(virtualPath, cdnPath)
    {
        Builder = new CustomScriptBundleBuilder();
    }
}

并创建另一个类来更改内容js文件如下::

And, create another class to change the content of the js files as follows::

class CustomScriptBundleBuilder : IBundleBuilder
{
    private string Read(BundleFile file)
    {
        //read file
        FileInfo fileInfo = new FileInfo(HttpContext.Current.Server.MapPath(@file.IncludedVirtualPath));
        using (var reader = fileInfo.OpenText())
        {
            return reader.ReadToEnd();
        }
    }

    public string BuildBundleContent(Bundle bundle, BundleContext context, IEnumerable<BundleFile> files)
    {
        var content = new StringBuilder();

        foreach (var fileInfo in files)
        {
            var contents = new StringBuilder(Read(fileInfo));
            //a regular expersion to get catch blocks
            const string pattern = @"\bcatch\b(\s*)*\((?<errVariable>([^)])*)\)(\s*)*\{(?<blockContent>([^{}])*(\{([^}])*\})*([^}])*)\}";

            var regex = new Regex(pattern);
            var matches = regex.Matches(contents.ToString());

            for (var i = matches.Count - 1; i >= 0; i--) //from end to start! (to avoid loss index)
            {
                var match = matches[i];
                //catch( errVariable )
                var errVariable = match.Groups["errVariable"].ToString();
                //start index of catch block
                var blockContentIndex = match.Groups["blockContent"].Index;
                var hasContent = match.Groups["blockContent"].Length > 2;

                contents.Insert(blockContentIndex,
                          string.Format("if(customErrorLogging)customErrorLogging({0}){1}", errVariable, hasContent ? ";" : ""));
            }

            var parser = new JSParser(contents.ToString());
            var bundleValue = parser.Parse(parser.Settings).ToCode();

            content.Append(bundleValue);
            content.AppendLine(";");
        }

        return content.ToString();
    }
}

现在,将js文件包含在应用程序包中class:

Now, include your js files in application Bundles with your class:

BundleTable.Bundles.Add(new CustomScriptBundle("~/scripts/vendor").Include("~/scripts/any.js"));

最后,在新的js文件中编写customErrorLogging函数,如下所述,并将其添加到项目的main中html表格:

Finally, in a new js file write customErrorLogging function as described below, and add it to your project's main html form:

"use strict";
var customErrorLogging = function (ex) {
    //do something
};

window.onerror = function (message, file, line, col, error) {
    customErrorLogging({
        message: message,
        file: file,
        line: line,
        col: col,
        error: error
    }, this);
    return true;
};

现在,您可以捕获应用程序中的所有异常并管理它们:)

Now, you can catch all exceptions in your application and manage them :)

这篇关于捕获服务器端的所有JavaScript客户端错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 10:01