我们使用window.onerror来捕获未处理的异常(为开发团队记录它们,并显示友好的用户警报).最近我们注意到,在谷歌浏览器中,如果错误消息超过一定长度就会被截断,并且文本" ...<omitted>..."被神秘地添加到错误消息中.
以下代码将演示此功能(在Chrome版本33.0.1750中).我想知道是否有其他人有这个问题?
<html>
<head>
<script type="text/javascript">
window.onerror = function (errorMsg, url, lineNumber) {
alert('Error: ' + errorMsg);
}
var throwError = function () {
throw new Error(
'Something went wrong. Something went wrong. Something went wrong. Something went wrong. ' +
'Something went wrong. Something went wrong. Something went wrong. Something went wrong. ' +
'Something went wrong. Something went wrong. Something went wrong. Something went wrong. ' +
'Some text gets truncated before this point!');
}
</script> …Run Code Online (Sandbox Code Playgroud) 我正在研究用Sencha Touch和PhoneGap编写的复杂Web应用程序.Sencha Touch应用程序的编写方式使得很难用try/catch包围每个潜在的故障点,尤其是异步AJAX请求处理程序.
检测未处理的异常何时发生因为它看起来和感觉就像使用PhoneGap的本机应用程序(即我们不能指望它们刷新页面并再次尝试在常规浏览器中查看的Web应用程序中),这一点也非常重要.
您能否建议如何处理这种情况?
我正在尝试在生产网站上记录javascript错误.到目前为止,它与网站中包含的以下代码相得益彰:
function catcherr(errorMessage, url, line) {
var parameters = "msg=" + escape(errorMessage)
+ "&url=" + escape(url)
+ "&line=" + escape(line);
new Image().src = "/error.gif?" + parameters;
return false;
};
window.onerror = catcherr;
Run Code Online (Sandbox Code Playgroud)
我正在尝试向错误添加堆栈跟踪以获取更多信息.这基本上与以下想法一起使用,包括上面的功能:
try { i.dont.exist += 0; } // does not exist - that's the point
catch (e)
{
if (e.stack) // Firefox
{
// do some stuff
Run Code Online (Sandbox Code Playgroud)
我使用jquery,一个简单的例子:
<script type="text/javascript">
jQuery(document).ready(function() {
p.foo += 1; // this should throw an error
// do stuff
});
</script>
Run Code Online (Sandbox Code Playgroud)
有趣的是,当我在jquery的"ready"函数中有错误时,"try {i.dont.exist …
我实施了:
window.onerror = function (m, s, l, c, e) {
}
Run Code Online (Sandbox Code Playgroud)
其中 e 是错误对象。例如,它包含:
ReferenceError: rde is not defined
at Object.bla.cs (domain.pt/bla.js:418:17)
at n.aanv (domain.pt/bla.js:125:29)
Run Code Online (Sandbox Code Playgroud)
如果我 make e.toString(),则只返回第一行。如何获得3条线?谢谢你。
我正在用JavaScript创建一个应用程序来创建一个新的图像.有时会失败.通过附加image.onerror事件监听器,我能够检测到它的次数.问题是:我如何了解发生了什么错误 - 图像的错误对象带来了哪些参数?到目前为止,我才发现了这一点
image.onerror = function (errorMsg, url, lineNumber, column, errorObj) {
console.log('Error: ' + errorMsg + ' Script: ' + url + ' Line: ' + lineNumber
+ ' Column: ' + column + ' StackTrace: ' + errorObj);
}
Run Code Online (Sandbox Code Playgroud)
我得到的: javascript:如何在弹出警报中显示脚本错误?
返回Error: [object Event] Script: undefined Line: undefined Column: undefined StackTrace: undefined并在错误对象中找不到任何与消息或错误代码相关的内容.
我想使用 onerror 来拦截和记录我的 JavaScript 应用程序中发生的所有错误。这按预期工作,除非使用承诺或异步函数。
下面通过剪断抛出的异常fail如预期,但被拦截rejectPromise和throwAsync的不是调用的onerror处理程序总是显示一个Uncaught (in promise) Error控制台?
如何始终拦截使用 promise 和异步函数的代码库中的所有错误?
window.onerror = function(message, source, lineno, colno, error) {
console.log('onerror handler logging error', message);
return true;
}
function rejectPromise() {
return Promise.reject(new Error('rejected promise'));
}
async function throwAsync() {
throw new Error('async exception');
}
function fail() {
throw new Error('exception');
}
rejectPromise().then(() => console.log('success'));
throwAsync();
fail();Run Code Online (Sandbox Code Playgroud)
当所有其他浏览器(IE7,8,9,FF,Opera和Safari)都重复调用它时,为什么Chrome只调用img元素的onerror事件?
有没有办法强制它再次重复攻击(在Chrome中)?
HTML:
<div id="thisWorks">
this works in Chrome. onerror event is called once.
<img src="http://www.asdfjklasdfasdf.com/bogus1.png"
onerror="fixit(this);"
rsrc="http://eatfrenzy.com/images/success-tick.png" />
</div>
<div id="thisDoesNotWork">
this does not work in Chrome. onerror event is not called twice.
<img src="http://www.asdfjklasdfasdf.com/bogus1.png"
onerror="fixit(this);"
rsrc="http://www.asdfjklasdfasdf.com/bogus2.png|http://eatfrenzy.com/images/success-tick.png" />
</div>
Run Code Online (Sandbox Code Playgroud)
JAVASCRIPT:
function fixit(img)
{
var arrPhotos = img.getAttribute('rsrc').split('|');
// change the img src to the next available
img.setAttribute('src', arrPhotos.shift());
// now put back the image list (with one less) into the rsrc attr
img.setAttribute('rsrc', arrPhotos.join('|'));
return true;
}
Run Code Online (Sandbox Code Playgroud)
编辑: …
在GNU Make 3.81中,我需要在工具链的任何部分发生错误时删除锁定文件.是否有特殊目标可以让我这样做?我需要编写包装脚本吗?
在下面的示例中,如果file.out的规则失败,我需要unlock_id.
谢谢!-Jeff
all: lock_id file.out unlock_id
file.out: file.in
file-maker < file.in > $@
lock_id:
lockfile file.lock
unlock_id:
rm -rf file.lock
Run Code Online (Sandbox Code Playgroud) 当我开发iPhone/iPad应用程序时,我总是使用崩溃报告系统.我目前最喜欢的是Crashlytics - 效果很好.
对于Android应用程序,我一直在使用相当基本的ACRA崩溃记者.
既然我正在构建一个在客户端自己的服务器上运行的webapp,那么构建某种崩溃报告系统似乎是一个好主意,可能与window.onerror事件相关联.
当然,我可以建立自己的系统.但有没有人知道一个发送和整理报告的好的插入式解决方案?
顺便说一句,我更喜欢完全客户端解决方案(即只是JavaScript),而不是需要服务器支持的任何东西.
我使用 Axios 和 react-query 的组合向服务器发出 POST 请求,该服务器可能会返回响应代码 400 和验证错误。
export const axiosDefault = axios.create({
baseURL: API_LINK,
headers: {
'Content-Type': 'application/json'
},
})
const contactMutation = useMutation(
(data) => axiosDefault.post('/contact', data),
{
onSuccess: (response) => {
console.log('Success', response)
},
onError: (error) => {
console.log('Error', error)
}
}
)
Run Code Online (Sandbox Code Playgroud)
但是,当调用contactMutation.mutate(someData)来自服务器的错误响应时,反应查询库不会处理错误响应,而是向上传播。onSuccess或处理程序都没有onError被调用,isError突变的属性也没有设置。
我花了几个小时为此烦恼,我做错了什么?
onerror ×10
javascript ×7
image ×2
stack-trace ×2
axios ×1
cordova ×1
crash ×1
events ×1
gnu-make ×1
html ×1
html5 ×1
jquery ×1
lockfile ×1
makefile ×1
object ×1
parameters ×1
promise ×1
react-query ×1
reporting ×1
sencha-touch ×1