在服务器端捕获所有 JavaScript 客户端错误
Catch all JavaScript client-side errors on the server-side
如何捕获客户端代码中发生的任何异常,例如 chrome 开发人员工具上的"捕获异常时暂停"?
我找到了解决方案!
我用过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();
}
}
并且,创建另一个类来更改 js 文件的内容,如下所示:
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 文件包含在您的类的应用程序包中:
BundleTable.Bundles.Add(new CustomScriptBundle("~/scripts/vendor").Include("~/scripts/any.js"));
最后,在一个新的js文件中编写自定义错误日志记录函数,如下所述,并将其添加到项目的主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;
};
现在,您可以捕获应用程序中的所有异常并对其进行管理:)
您可以使用 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
}
顾名思义,您尝试在"try"块中执行一些代码。如果抛出错误,您可以在"catch"块中执行特定任务(例如,以特定方式记录错误)。
还有更多选项可用:您可以有多个"catch"块,具体取决于抛出的错误类型等。更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
查看一个小示例,如何捕获异常:
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>
将代码放入 try Block 并尝试在 catch Block 中捕获错误。
相关文章:
- 如何使用Javascript客户端对象模型检索Sharepoint 2010列表项权限
- 从javascript客户端验证REST服务
- 无法使用javascript客户端访问远程web服务
- 在javascript客户端和java服务器之间共享Google Analytics ClientID
- 应用程序引擎终结点:用于OAuth2授权的Javascript客户端库需要多个弹出窗口
- 使用Javascript客户端进行REST基本身份验证的安全缺陷是什么(如果有的话)
- 如何从Python服务器获取到Javascript客户端
- 在Javascript客户端中隐藏基本的身份验证凭据
- Postback与Javascript客户端计算
- 有没有办法使用Google's Javascript客户端登录时没有弹出窗口
- javascript客户端ORM的框架
- 如何使用javascript客户端设置Python服务器端
- Firebase服务器发送事件-如何构建Java/JavaScript客户端
- Javascript客户端从ASP.NET MVC后端延迟加载模型
- 当将从javascript客户端调用javaapi时,在javaapi中使用2d数组
- javascript客户端中的服务器端数据绑定
- 在javascript(客户端)中跨多个窗口共享数据
- Javascript客户端提供哪些数据库,不包括特定于浏览器的数据库
- 如何将Node.js服务器数据发送到Javascript客户端
- javascript客户端安全性