CocosCreator(2.0.10) 全局捕捉异常

为了方便捕捉游戏在终端运行时发生的异常,可以在全局任意地方添加如下代码,报错时上报exception给服务器,方便记录

if(cc.sys.isNative) {
	window.__errorHandler = function (errorMessage, file, line, message, error) {
		let exception = {};
		exception.errorMessage = errorMessage;
		exception.file = file;
		exception.line = line;
		exception.message = message;
		exception.error = error;
		if (window.exception != JSON.stringify(exception)) {
			window.exception = JSON.stringify(exception);
			console.log(exception);
			//TODO: 发送请求上报异常
		}
	};
} else if(cc.sys.isBrowser) {
	window.onerror = function (errorMessage, file, line, message, error) {
		let exception = {};
		exception.errorMessage = errorMessage;
		exception.file = file;
		exception.line = line;
		exception.message = message;
		exception.error = error;
		if (window.exception != JSON.stringify(exception)) {
			window.exception = JSON.stringify(exception);
			console.log(exception);
			//TODO: 发送请求上报异常
		}
		
		
	};
}

 

你可能感兴趣的:(CocosCreaor)