这是一个片段,可从xhr-get请求中检测与网络无关的错误,并在控制台中输出有关此错误的一些信息。
还有一个额外的isevalError()函数可以处理所有eval-
error类型……我并不为此感到骄傲。更好的方法是获取errorMessage子类的父对象。我认为您通常可以放弃isevalError(),因为在此块中不应有任何其他错误。
function isevalError(errorMessage){ return errorMessage.name == "RangeError" || errorMessage.name == "ReferenceError" || errorMessage.name == "SyntaxError" || errorMessage.name == "URIError" || errorMessage.name == "TypeError";}var foo = dojo.xhrGet({ url: 'stacko.js', handleAs: "javascript", load: function(returnValue) { console.log("load: "+returnValue); }, error: function(errorMessage,ioargs) { //request worked fine, this must be a non-network related error if(ioargs.xhr.readyState == 4 && ioargs.xhr.status == 200) { if(isevalError(errorMessage)){ //show eval-error, url request & the JS pre that causes the exception //eval-error types: RangeError,ReferenceError,SyntaxError, URIError, TypeError console.error(errorMessage.name+" in JS Code detected: (url: "+ioargs.url+")") console.error("Error Message: "+ errorMessage); console.error(ioargs.xhr.responseText); } //a little reflection - if u want to know who triggered this error //(although in this case the output is not very helpful ) console.error("Error triggered by: "+arguments.callee.caller.toString()); //last but not least log the error & the xhr-request object for more information console.error("XHR Object:"); console.error(ioargs); console.error("Error Object:"); console.error(errorMessage); } }});


