回答

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