回答

1

我找到了解决方案!

我已经使用了C#和MVC。

添加一个新类来定制你的js文件打包这样的:

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(); 
    } 
} 

而且,创建另一个类如下::

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文件的内容,包括在应用程序软件包,您的js文件与你的类:

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

最后,在一个新的js文件WRI如下所述TE customErrorLogging功能,并将其添加到项目的主HTML表单:

"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; 
}; 

现在,你能赶上在应用程序中所有的异常,并进行管理:)

0

你可以使用try/catch块:

try { 
    myUnsafeFunction(); // this may cause an error which we want to handle 
} 
catch (e) { 
    logMyErrors(e); // here the variable e holds information about the error; do any post-processing you wish with it 
} 

正如其名称所示,你尝试在 “尝试” 块执行一些代码。如果发生错误,您可以在“catch”块中执行特定任务(例如,以特定方式记录错误)。

许多更多的选择:你可以取决于被抛出的错误类型等 更多的信息在这里多“捕捉”块:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

+0

感谢您的回复,但我想捕捉像window.onerror这样的函数中的任何异常,即使我使用了try catch块,而无需修改应用程序代码! – Pedram 2014-12-05 06:37:34

+0

我不知道我明白你想达到什么目的。你想在一个函数中处理所有的错误?那你有没有回答你自己的问题?不window.onerror适合您的需求? – TanguyP 2014-12-05 14:47:33

+0

是的,你明白,但是当我们在代码中使用try/catch块时window.onerror不会引发。 – Pedram 2014-12-07 21:33:15

0

看一个小例子,你如何捕捉异常:

try { 
 
alert("proper alert!"); 
 
    aert("error this is not a function!"); 
 
} 
 
catch(err) { 
 
    document.getElementById("demo").innerHTML = err.message; 
 
}
<body> 
 

 
<p id="demo"></p> 
 

 
</body>

把你的代码试图阻止,并试图抓住错误的catch块。

+0

感谢您的回复,但我想捕获window.onerror等函数中的任何异常,即使我使用了try catch块,而不需要修改应用程序代码! – Pedram 2014-12-05 06:36:23