Skip to content

Commit

Permalink
feat: finish addeventer ajax error and report
Browse files Browse the repository at this point in the history
  • Loading branch information
MriLiuJY committed Jun 25, 2019
1 parent 10a6f2f commit ac6d7ca
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 28 deletions.
19 changes: 8 additions & 11 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,16 @@ <h1>Monitor testPage</h1>

// fundebug.test();
// console.log(initMonitor);
window.addEventListener("error", function(err) {
console.log(err);
});

window.addEventListener('ajaxTimeout', function (e) {
console.log("ajaxTimeout");
console.log(e.detail); // XMLHttpRequest Object
});
// window.addEventListener('ajaxTimeout', function (e) {
// console.log("ajaxTimeout");
// console.log(e.detail); // XMLHttpRequest Object
// });

window.addEventListener('ajaxLoad', function (e) {
console.log("ajaxLoad");
console.log(e.detail); // XMLHttpRequest Object
});
// window.addEventListener('ajaxLoad', function (e) {
// console.log("ajaxLoad");
// console.log(e.detail); // XMLHttpRequest Object
// });

initMonitor(config);
</script>
Expand Down
5 changes: 5 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ app.use("/servererr", (req, res) => {
// timeout no res
app.use("/timeout", (req, res) => {});

// timeout no res
app.use("/monitor", (req, res) => {
res.send({});
});

const point = 9998;
console.log("Your server listen at " + "http://localhost:" +point + "/index");
app.listen(point);
Expand Down
6 changes: 6 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ app.use("/servererr", (req, res) => {
app.use("/timeout", (req, res) => {});


// timeout no res
app.use("/monitor", (req, res) => {
res.send({});
});


const point = 9999;
console.log("打包测试地址: " + "http://localhost:" +point + "/index");
app.listen(point);
Expand Down
2 changes: 2 additions & 0 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* @version 0.0.1-beta
*/


/* eslint-disable */
const ajax = (function() {
return {
canAjax: function() {
Expand Down
47 changes: 34 additions & 13 deletions src/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,55 @@
*/

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

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

// js 内部运行错误
function GetError(err) {
//
export const ajaxError = function(err) {
console.log("ajaxError ------", err);
// 处理err 上报
let wrap = new Wrap();
let data = wrap._geWrap();
wrap._getIP(
function(ip) {
data.ip = ip;
}
);
if (err.type === "ajaxLoad" && err.detail.status > 300) {
data.responseURL = err.detail.responseURL;
data.status = err.detail.status;
data.statusText = err.detail.statusText;
console.log(data);
if (ajax.canAjax()) {
ajax.post({type: "post"}, "/monitor", data,
function() {},
function(err) {
console.log(err);
});
}
}
}

// js 内部运行错误
export const getError = function(err) {
// 可以被取消的是js抛出的错误
if (err.cancelable) {
new GetJsError(err);
getJsError(err);
} else {
new GetResourceError(err);
geetResourceError(err);
}
}

// js 抛出的错误
function GetJsError(err) {
const getJsError = function(err) {
console.log(err);
// 处理err 上报
}

// 资源加载错误
function GetResourceError(err) {
const geetResourceError = function (err) {
console.log(err);
// 处理err 上报
}

module.exports = {
GetError,
GetServerError,
};
24 changes: 20 additions & 4 deletions src/initMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Wrap from "./wrap";
import Config from "./config";
import { GetError } from "./error";
import { getError, ajaxError } from "./error";

/* eslint-disable */
function InitMonitor(userConfig) {
Expand All @@ -17,7 +17,6 @@ function InitMonitor(userConfig) {
// 获取 wrap
let wrap = new Wrap();
let data = {};
data.wrap = wrap._geWrap();
wrap._getIP(
function(ip) {
data.ip = ip;
Expand All @@ -31,11 +30,11 @@ InitMonitor.prototype = {
_initListenJS: function() {
// 监听全局下的error事件
window.addEventListener("error", function(err) {
new GetError(err);
getError(err);
}, true);
},
_initListenAjax: function () {
console.log("_initListenAjax");
let self = this;
function ajaxEventTrigger(event) {
var ajaxEvent = new CustomEvent(event, { detail: this });
window.dispatchEvent(ajaxEvent);
Expand All @@ -62,6 +61,23 @@ InitMonitor.prototype = {
}

window.XMLHttpRequest = newXHR;
self._startLintenAjax();
},
_startLintenAjax() {
// ajax error
window.addEventListener("error", function(err) {
ajaxError(err);
});

// ajax timeout
window.addEventListener("ajaxTimeout", function(err) {
ajaxError(err);
});

// ajax load error
window.addEventListener("ajaxLoad", function(err) {
ajaxError(err);
});
},
_send: function () {},
}
Expand Down

0 comments on commit ac6d7ca

Please sign in to comment.