UNPKG

gdy-report

Version:

Guangdianyun frontend sentry report tool

1 lines 7.68 kB
"use strict";function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var Sentry=require("@sentry/browser");function Report(e){try{var t=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};setTimeout((function(){"info"===e?(Sentry.setExtra("data",r),Sentry.captureMessage(t,"info")):"error"===e&&(Sentry.setExtra("data",r),Sentry.captureException(new Error(t))),Promise.resolve().then((function(){n()}))}),i.outtime)},r=function(e,t){var r=t.responseURL;if(!o.some((function(e){return r.includes(e)})))switch(e){case"done":Sentry.setExtra("data",t),Sentry.captureException(new Error("Api Error:".concat(t.msg||t.statusText)));break;case"error":Sentry.setExtra("data",t),Sentry.captureException(new Error("Request Error:".concat(t.statusText)))}},n=function(){window.performance&&window.performance.clearResourceTimings&&performance.clearResourceTimings()},o=["/sockjs-node/info","arms-retcode.aliyuncs.com","aliyuncs.com","ynuf.aliapp.org"],i={dsn:"",version:"1.0.0",env:"",appid:"",uin:0,name:"",outtime:300,filterUrl:[],apiRules:[],isPage:!1,isAjax:!0,isError:!0};Object.assign(i,e),i.filterUrl=i.filterUrl.concat(o),i.apiRules=i.apiRules.concat([{url:"guangdianyun.tv",rules:{data:{name:"data",permission:[]},identify:{name:"errorCode",permission:[0,1]},msg:{name:"errorMessage",permission:[]}}}]);var a="";switch(i.env){case"TEST":a="development";break;case"PRE":a="preview";break;case"":default:a="production"}Sentry.init({dsn:i.dsn,release:i.version,environment:a}),Sentry.setUser({AppId:i.appid,Uin:i.uin,Name:i.name,Environment:a}),Sentry.setTag("Package",require("../package.json").version),Sentry.setTag("Uin",i.uin),Sentry.setTag("Appid",i.Appid),i.isError&&function(){window.addEventListener("error",(function(e){var r={t:(new Date).getTime(),msg:e.target.localName+" is load error",target:e.target.localName,type:e.type,resourceUrl:e.target.href||e.target.currentSrc};t("error","Resource Error",r)}),!0),window.onerror=function(e,r,n,o,i){setTimeout((function(){o=o||window.event&&window.event.errorCharacter||0;var a={t:(new Date).getTime(),msg:i&&i.stack?i.stack.toString():e,resourceUrl:r,line:n,col:o};t("error","Script Error",a)}),0)},window.addEventListener("unhandledrejection",(function(e){var r,n,o,i=e&&e.reason,a=i.hasOwnProperty("message")?i.message:"",s=(i.stack||"").match(/\(.+?\)/);s&&s.length&&(s=(s=(s=s[0]).replace(/\w.+[js|html]/g,(function(e){return r=e,""}))).split(":")),s&&s.length>1&&(o=parseInt(s[1]||0,10)),s&&s.length>2&&(n=parseInt(s[2]||0,10));var c={t:(new Date).getTime(),msg:a,resourceUrl:r,line:n,col:o};t("error","Unhandledrejection",c)}));var e=console.error;console.error=function(r){return setTimeout((function(){var e={t:(new Date).getTime(),msg:r,resourceUrl:location.href};t("error","Console Error",e)}),0),e.apply(console,arguments)}}(),(i.isAjax||i.isError)&&function(e){function t(t){return function(){var r=this.hasOwnProperty(t+"_")?this[t+"_"]:this.xhr[t],n=(e[t]||{}).getter;return n&&n(r,this)||r}}function r(t){return function(r){var n=this.xhr,o=this,i=e[t];if("function"==typeof i)try{n[t]=function(){e[t](o)||r.apply(n,arguments)}}catch(e){}else{var a=(i||{}).setter;r=a&&a(r,o)||r;try{n[t]=r}catch(e){this[t+"_"]=r}}}}function n(t){return function(){var r=[].slice.call(arguments);if(!e[t]||!e[t].call(this,r,this.xhr))return this.xhr[t].apply(this.xhr,r)}}window._ahrealxhr=window._ahrealxhr||XMLHttpRequest,XMLHttpRequest=function(){for(var e in this.xhr=new window._ahrealxhr,this.xhr){var o="";try{o=_typeof(this.xhr[e])}catch(e){}"function"===o?this[e]=n(e):Object.defineProperty(this,e,{get:t(e),set:r(e)})}},window._ahrealxhr}({onreadystatechange:function(e){if(4===e.xhr.readyState){var t=e.xhr.responseURL?e.xhr.responseURL:"";if(i.filterUrl.some((function(e){return t.includes(e)}))||!t)return;setTimeout((function(){if(e.xhr.status<200||e.xhr.status>300){e.method=e.args.method;var n={status:e.xhr.status,statusText:e.xhr.statusText||"",method:e.args.method,responseURL:e.args.url,data:e.xhr.response||null,msg:""};r("done",n)}else!function(e,t){var n=i.apiRules.filter((function(t){return e.includes(t.url)}));if(n.length>1)return!1;if(!n)return!1;var o=n[0].rules||null;if(!o)return!1;var a={};if(t.xhr.response&&"string"==typeof t.xhr.response&&t.xhr.response.length>0)try{var s=JSON.parse(t.xhr.response);Object.assign(a,s)}catch(e){}var c=!1;for(var u in o)0===o[u].permission.length||o[u].permission.includes(a[o[u].name])||(c=!0);var d={status:t.xhr.status,statusText:t.xhr.statusText||"",method:t.args.method,responseURL:t.args.url,data:a[o.data.name]||null,identify:a[o.identify.name]||null,msg:a[o.msg.name]||""};c&&r("done",d)}(t,e)}),600)}},onerror:function(e){if(e.args){var t=e.args.url?e.args.url:"";if(i.filterUrl.some((function(e){return t.includes(e)}))||!t)return;var n={method:e.args.method,responseURL:t,statusText:"XHR request error"};r("error",n)}},open:function(e,t){this.args={url:e[1],method:e[0]||"GET",type:"xmlhttprequest"},n()}}),window.addEventListener("load",(function(){i.isPage&&function(){if(window.performance){var e=performance.timing,r={width:document.documentElement.clientWidth||document.body.clientWidth,height:document.documentElement.clientHeight||document.body.clientHeight,dnst:e.domainLookupEnd-e.domainLookupStart||0,tcpt:e.connectEnd-e.connectStart||0,wit:e.responseStart-e.navigationStart||0,domt:e.domContentLoadedEventEnd-e.navigationStart||0,lodt:e.loadEventEnd-e.navigationStart||0,radt:e.fetchStart-e.navigationStart||0,rdit:e.redirectEnd-e.redirectStart||0,uodt:e.unloadEventEnd-e.unloadEventStart||0,reqt:e.responseEnd-e.requestStart||0,andt:e.domComplete-e.domInteractive||0};t("info","Page Performance",r)}}()}),!1)}catch(e){}}Report.init=function(e){var t={dsn:"",version:"1.0.0",env:""};Object.assign(t,e);var r="";switch(t.env){case"TEST":r="development";break;case"PRE":r="preview";break;case"":default:r="production"}Sentry.init({dsn:String(t.dsn),release:t.version,environment:r})},Report.setUser=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"",n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"",o="";switch(n){case"TEST":o="development";break;case"PRE":o="preview";break;case"":default:o="production"}Sentry.setUser({AppId:e,Uin:t,Name:r,Environment:o})},Report.api=function(e,t,r){var n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{};Sentry.configureScope((function(r){r.setTag("appid",e),r.setTag("uin",t)})),Sentry.setTag("Uin",t),Sentry.setTag("Appid",e),Sentry.setExtra("data",n),Sentry.captureException(new Error("Api Error:".concat(r)))},Report.info=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"Info",n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{};Sentry.configureScope((function(r){r.setTag("appid",e),r.setTag("uin",t)})),Sentry.setTag("Uin",t),Sentry.setTag("Appid",e),Sentry.setExtra("data",n),Sentry.captureMessage(r,"info")},Report.error=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"New Error",n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{};Sentry.configureScope((function(r){r.setTag("appid",e),r.setTag("uin",t)})),Sentry.setTag("Uin",t),Sentry.setTag("Appid",e),Sentry.setExtra("data",n),Sentry.captureException(new Error(r))},"object"===("undefined"==typeof exports?"undefined":_typeof(exports))?module.exports=Report:"function"==typeof define&&define.amd?define([],(function(){return Report})):window.Report=Report;