UNPKG

tsp-component

Version:

提供多端和react版本的UI组件

152 lines (151 loc) 5.79 kB
import { userAgent } from '../util/device'; import { urlStartIndexForString, getCurrentRoute } from '../util/router'; var Exception = (function () { function Exception() { } Exception.send = function (data) { if (process.env.NODE_ENV !== 'production' || !Exception.productCode) { if (Exception.productCode) { console.log('异常上报的内容:'); console.log(data); } return; } var params = { col: data.col, errorType: data.errorType || 1, line: data.line, content: data.content, details: data.details, productCode: data.productCode || Exception.productCode, url: data.url, userAgent: userAgent, lat: Exception.lat, lng: Exception.lng, account: Exception.account, openId: Exception.openId, version: Exception.version, currentPage: getCurrentRoute(), network: Exception.network }; this.webapi.post({ api: Exception.apiname, type: 'post', contentType: 'application/json', timeoutStr: 'none', params: params, success: function () { return null; }, isCache: false }); }; Exception.catchParse = function (e) { var details = e.stack.trim(); var startIndex = urlStartIndexForString(details); var suffixIndex = details.indexOf('.js:') + 4; var lineEndIndex = details.substr(suffixIndex).indexOf(':') + suffixIndex; var endIndex = lineEndIndex + 6; var path = details.substr(startIndex, endIndex - startIndex); if (urlStartIndexForString(path, true) !== startIndex) { startIndex = urlStartIndexForString(path, true); path = path.substr(startIndex, endIndex - startIndex); startIndex = 0; suffixIndex = path.indexOf('.js:') + 4; lineEndIndex = path.substr(suffixIndex).indexOf(':') + suffixIndex; endIndex = lineEndIndex + 6; } var url = path.substr(startIndex, suffixIndex - startIndex - 1); var line = path.substr(suffixIndex, lineEndIndex - suffixIndex); var col = path.substr(lineEndIndex, endIndex - suffixIndex).replace(/[^0-9]/ig, ''); return { col: parseInt(col), line: parseInt(line), url: url, details: e.stack, content: "name: " + e.name + ";\nmessage: " + e.message + ";", errorType: 1, userAgent: userAgent, lat: Exception.lat, lng: Exception.lng, account: Exception.account, openId: Exception.openId, version: Exception.version, currentPage: getCurrentRoute(), network: Exception.network }; }; Exception.catchSend = function (e) { var data = Exception.catchParse(e); if (process.env.NODE_ENV !== 'production') { console.log('异常解析结果:'); console.log(data); throw e; } else { Exception.send(data); } }; Exception.listen = function () { window.onerror = function (msg, url, line, col, error) { if (msg !== 'Script error.' && !url) { return true; } setTimeout(function () { var data = { productCode: Exception.productCode, errorType: 1, col: 0, line: 0, url: '', content: '', details: '', userAgent: userAgent, lat: Exception.lat, lng: Exception.lng, account: Exception.account, openId: Exception.openId, version: Exception.version, currentPage: getCurrentRoute(), network: Exception.network }; col = col || (window.event && window.event['errorCharacter']) || 0; data.url = url; data.line = line; data.col = col; if (!!error && !!error.stack) { data.content = "name: " + error.name + ";\nmessage: " + error.message + ";"; data.details = error.stack; } else if (!!arguments.callee) { var ext = []; var f = arguments.callee.caller; var c = 3; while (f && (--c > 0)) { ext.push(f.toString()); if (f === f.caller) { break; } f = f.caller; } ext = ext.join(','); data.content = ext; } if (process.env.NODE_ENV !== 'production') { console.log('onerror扑捉到的错误:'); console.log(data); } else { Exception.send(data); } }, 0); if (process.env.NODE_ENV !== 'production') { throw error; } else { return true; } }; }; Exception.apiname = '/api/product/exception/create'; return Exception; }()); export default Exception;