Skip to content

Commit

Permalink
feat: add report rrweb event data with error request
Browse files Browse the repository at this point in the history
  • Loading branch information
MriLiuJY committed Aug 5, 2019
1 parent 440a09d commit cf64d79
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
49 changes: 35 additions & 14 deletions src/error.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,65 @@
/**
* @file error event center
* @author JYkid
* @author JYkidrecord
* @version 0.0.1-beta
*/

/* eslint-disable */
import { ajax } from "./ajax";
import Wrap from "./wrap";

const wrap = new Wrap();
const getErrorData = function(err, initMonitor) {
let data = wrap._getErrorMessage(err);
data.record = [];

let config = initMonitor._config;
if (config.record) {
data.record = initMonitor._getRrwebEvent();
}
return data;
};

// 服务端返回错误
export const getServerError = function() {};

// ajaxError
export const ajaxError = function(err, config) {
export const ajaxError = function(err, initMonitor) {
// 处理err 上报
if (err.type === "ajaxLoad" && err.detail.status >= 400) {
let data = new Wrap()._getErrorMessage(err);
ajax.post(config.protocol + config.url, data, function() {},
let data = getErrorData(err, initMonitor);
let config = initMonitor._config;
ajax.post(config.protocol + config.url, data, function() {
initMonitor._clearEvent();
},
function(error) {
console.log(error);
});
}
}

// js 抛出的错误
export const getJsError = function(err, config) {
let data = new Wrap()._getErrorMessage(err);
export const getJsError = function(err, initMonitor) {
let data = getErrorData(err, initMonitor);
let config = initMonitor._config;
ajax.post(config.protocol + config.url, data,
function() {},
function() {
initMonitor._clearEvent();
},
function(error) {
console.log(error);
});
}

// 资源加载错误
export const geetResourceError = function (err, config) {
let data = new Wrap()._getErrorMessage(err);
ajax.post(config.protocol + config.url, data,
function() {},
function(error) {
console.log(error);
});
export const geetResourceError = function (err, initMonitor) {
let data = getErrorData(err, initMonitor);
let config = initMonitor._config;
ajax.post(config.protocol + config.url, data,
function() {
initMonitor._clearEvent();
},
function(error) {
console.log(error);
});
}
17 changes: 12 additions & 5 deletions src/initMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ InitMonitor.prototype = {

// 监听全局下的 Promise 错误
let unhandledrejection = function(err){
getJsError(err, self._config);
getJsError(err, self);
}
window.addEventListener("unhandledrejection", unhandledrejection);
self._setEvent({
Expand All @@ -51,11 +51,11 @@ InitMonitor.prototype = {
if (err.filename.indexOf('monitor') > -1 || process.env.NODE_ENV === 'development') {
return;
} else {
getJsError(err, self._config);
getJsError(err, self);
}
} else {
// 静态资源加载的error事件
geetResourceError(err, self._config);
geetResourceError(err, self);
}
}
window.addEventListener("error", errorEvent, true);
Expand Down Expand Up @@ -99,7 +99,7 @@ InitMonitor.prototype = {

// ajax timeout
let ajaxTimeout = function(err) {
!(err.detail.responseURL.indexOf(self._config.url) > -1) && ajaxError(err, self._config);
!(err.detail.responseURL.indexOf(self._config.url) > -1) && ajaxError(err, self);
};
window.addEventListener("ajaxTimeout", ajaxTimeout);
self._setEvent({
Expand All @@ -109,7 +109,7 @@ InitMonitor.prototype = {

// ajax load error
let ajaxLoad = function(err) {
!(err.detail.responseURL.indexOf(self._config.url) > -1) && ajaxError(err, self._config);
!(err.detail.responseURL.indexOf(self._config.url) > -1) && ajaxError(err, self);
}
window.addEventListener("ajaxLoad", ajaxLoad);
self._setEvent({
Expand All @@ -129,6 +129,13 @@ InitMonitor.prototype = {
const self = this;
self._eventCenter._set(event);
},
/**
* clear rrweb event
*/
_clearEvent() {
const self = this;
self._eventCenter._clearRecord();
},
/**
* init rrweb
*/
Expand Down
2 changes: 1 addition & 1 deletion src/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Wrap.prototype = {
total++
}
return stack;
}
},
}

module.exports = Wrap;

0 comments on commit cf64d79

Please sign in to comment.