xys-monitor
Version:
A project named xys-monitor
124 lines (95 loc) • 4.07 kB
JavaScript
import { ignoreCheck, isType, myAjax } from './util'
import config from './config'
import Limit from './limit'
class Report {
constructor (reportSys) {
this.reportSys = reportSys
this.errs = []
this.limit = Limit
}
reportError (postdata, xhr = null) {
let key = postdata.errorMsg + postdata.errorType + postdata.columnNo + postdata.lineNo + postdata.pageUrl
//频率限制
if (!this.limit.check(key)) {
return
}
let ignoreList = this.reportSys.options.ignoreList
let pageId = this.reportSys.options.pageId
// let url = this.reportSys.options.url
let url = config.url
let sample = this.reportSys.options.sample || 100
let random = parseInt(Math.random() * 100)
if (random > sample) {
console.log('this error is over sample ' + sample)
return
}
if (!pageId) {
throw new Error('请先设置上报页面pageId')
}
if (!url) {
throw new Error('请先设置上报url')
}
if (ignoreList && !isType(ignoreList, 'Array')) {
console.log('ignore 请传入Array')
} else {
ignoreList = ignoreList || []
}
const errTxtForSplit = '--错误错误信息--'
if (postdata.errorMsg != null && xhr && xhr.xhrTraceId) {
postdata.errorMsg = postdata.errorMsg + `${errTxtForSplit}(traceId: ${xhr.xhrTraceId})`
}
try {
const extErrorMsg = this.reportSys.extraErrorMsg
if (postdata.errorMsg != null && extErrorMsg && Object.keys(extErrorMsg).length) {
const string = JSON.stringify(extErrorMsg)
if (postdata.errorMsg.indexOf(errTxtForSplit) > -1) {
postdata.errorMsg = postdata.errorMsg + `(extraMsg: ${string})`
} else {
postdata.errorMsg = postdata.errorMsg + `${errTxtForSplit}(extraMsg: ${string})`
}
}
} catch (err) {
console.log('handle extraErrorMsg error', err)
}
postdata = postdata && this.handleMes(postdata)
this.errs.push(postdata)
if (ignoreCheck(ignoreList, (postdata.errorMsg || '') + (postdata.errorType || ''))) {
return
}
//document.body必须在window.onload之后才能获取到
if (/backendalert/gi.test(postdata.errorMsg)) {
let msg = `${postdata.errorMsg.replace(/"|"/gi, '')}`
if (window.__memento_record && window.__memento_record.record && window.__memento_record.record.getTrackId()) {
msg += `,trackId=${window.__memento_record.record.getTrackId()}`
}
if (window._xystj && window._xystj.session) {
msg += `,si=${window._xystj.session}`
}
myAjax({
type: 'post',
url: config.backendalertUrl,
data: { errMsg: msg }
})
} else {
myAjax({
type: 'post',
url,
data: postdata
})
}
}
handleMes (postdata) {
let exadata = {}
postdata.pageUrl = location.href
exadata.pageId = this.reportSys.options.pageId
exadata.device = (window.screen.width || window.screen.availWidth) + '*' + (window.screen.height || window.screen.availHeight) + ' @ ' + (window.devicePixelRatio || 1) + 'x'
if (window.__memento_record && window.__memento_record.record && window.__memento_record.record.getTrackId()) {
exadata.trackId = window.__memento_record.record.getTrackId()
}
if (window._xystj && window._xystj.session) {
exadata.si = window._xystj.session
}
return Object.assign({}, exadata, postdata)
}
}
export default Report