【簡易版的前端監控系統】
1、Promise的錯誤如何監控?–promise不是所有都是接口請求
2、接口的報錯如何監控?–全局監控sdk,不改動公共的請求方法、不改動業務代碼;一般接口使用axios請求
3、資源的報錯如何監控?
4、監控: 埋點上報報錯
注意:
(1)埋點監控報錯死循環報錯 – 重試機制、另一個埋點
(2)運行監控代碼如何判斷Vue/React,Vue/React有無內部監控api直接調用?
(3)window.error?? 能否捕獲到接口的錯誤?
(4)所有監控放到同一個SDK監控
答:
(2)判斷是否存在 Vue/React 可以通過檢查 window.Vue
和 window.React
是否定義。
Vue: 有內部監控 API,可通過 Vue.config.errorHandler
捕獲 Vue 實例中的錯誤。
React: 類組件可用 ErrorBoundary 捕獲子組件錯誤,函數組件實驗性地能用 useErrorBoundary Hook 。
(3)window.onerror
不能捕獲接口的錯誤。接口請求通常使用 XMLHttpRequest
或 fetch
,其錯誤會在各自的回調或 Promise
中處理,不會觸發 window.onerror
。
- 【整體思路】:
SDK 監控錯誤是通過多種方式實現的,具體如下:
try...catch
:用于在可預見的代碼塊中捕獲特定錯誤,例如在模擬埋點上報時捕獲可能出現的錯誤。
window.onerror
:用于捕獲預料之外的同步錯誤,不過不能捕獲異步錯誤。
window.unhandledrejection
:專門用于監聽和捕獲未處理的 Promise 錯誤。
(在業務代碼里,通常:使用 Promise .catch() 處理 Promise 錯誤;使用 async/await 結合 try…catch 處理 Promise 錯誤)
網絡錯誤捕獲:
(1)XMLHttpRequest:重寫 window.XMLHttpRequest
并監聽其 error
事件,捕獲 XMLHttpRequest 請求的網絡錯誤。
(2)Axios:使用 Proxy 代理重寫 axios.request
方法,捕獲 Axios 請求的網絡錯誤。
資源加載錯誤捕獲:
重寫 window.addEventListener
方法,監聽 error
事件,捕獲 HTML 資源(如腳本、樣式表、圖片)加載失敗的錯誤。
// 定義前端監控 SDK 類
class FrontendMonitoringSDK {constructor(options) {this.options = options;this.init();this.monitorVueErrors();this.monitorReactErrors();}// 初始化監控init() {this.monitorPromiseErrors();this.monitorApiErrors();this.monitorResourceErrors();this.monitorWindowErrors();if (this.options.track) {this.monitorTrackErrors(this.options.track);}}// 監控 Promise 錯誤 -- Promise內部無需重試機制,上報前端監控仍然使用retryReport/*** 通常不建議對 Promise 錯誤使用重試機制。原因:Promise 錯誤一般是由代碼邏輯錯誤、異步操作的異常(如數據庫查詢失敗、函數調用參數錯誤)等引發的。重試并不能解決這些根源問題,反而可能導致程序陷入無限重試的循環,消耗大量資源。例如,在處理 Promise 時,如果是因為傳入的參數不符合要求而拋出錯誤,重試同樣的操作依舊會失敗。*/monitorPromiseErrors() {window.addEventListener('unhandledrejection', (event) => {this.retryReport({type: 'promise',message: event.reason instanceof Error ? event.reason.message : String(event.reason),stack: event.reason instanceof Error ? event.reason.stack : null});});}// 監控接口錯誤monitorApiErrors() {const originalXHR = window.XMLHttpRequest;window.XMLHttpRequest = function () {const xhr = new originalXHR();const self = this;xhr.addEventListener('error', function () {self.retryReport({type: 'api',message: `API 請求錯誤: ${xhr.status} ${xhr.statusText}`,url: xhr.responseURL});});return xhr;}.bind(this);if (window.axios) {const originalAxios = window.axios;const maxRetries = 3;window.axios = new Proxy(originalAxios, {get(target, prop) {if (prop === 'request') {return function (config) {let retries = 0;const makeRequest = () => {return originalAxios.request(config).catch((error) => {if (retries < maxRetries) {retries++;return makeRequest();} else {this.retryReport({type: 'api',message: `Axios 請求錯誤: ${error.message}`,url: config.url});throw error;}});};return makeRequest();}.bind(this);}return target[prop];}});}}// 監控資源加載錯誤monitorResourceErrors() {const maxRetries = 3;const originalAddEventListener = window.addEventListener;window.addEventListener = function (type, listener, options) {if (type === 'error') {const newListener = (event) => {if (event.target instanceof HTMLScriptElement || event.target instanceof HTMLLinkElement || event.target instanceof HTMLImageElement) {let retries = 0;const retryResourceLoad = () => {if (retries < maxRetries) {if (event.target instanceof HTMLScriptElement) {const src = event.target.src;event.target.src = '';event.target.src = src;} else if (event.target instanceof HTMLLinkElement) {const href = event.target.href;event.target.href = '';event.target.href = href;} else if (event.target instanceof HTMLImageElement) {const src = event.target.src;event.target.src = '';event.target.src = src;}retries++;} else {this.retryReport({type: 'resource',message: `資源加載錯誤: ${event.target.src || event.target.href}`,url: event.target.src || event.target.href});}};retryResourceLoad();} else {listener.call(this, event);}};return originalAddEventListener.call(this, type, newListener, options);}return originalAddEventListener.call(this, type, listener, options);}.bind(this);}// 監控全局錯誤/**1. message: 錯誤的具體描述信息2. source: 發生錯誤的腳本文件的 URL;如果錯誤出現在內聯腳本中,返回當前頁面的 URL。3. lineno: 錯誤發生所在行的行號4. colno 錯誤發生所在列的列號5. error: 一個 Error 對象,它包含了更詳盡的錯誤信息,像錯誤堆棧(stack)之類的。*/monitorWindowErrors() {window.onerror = (message, source, lineno, colno, error) => {this.retryReport({type: 'window',message: message,stack: error ? error.stack : null,source: source,lineno: lineno,colno: colno});return true;};}// 監控埋點庫上報錯誤monitorTrackErrors(track) {const { Track, config, errorType } = track;const maxRetries = 3;const trackInstance = new Track(config);// 假設庫有一個錯誤回調trackInstance.onError = (error) => {let retries = 0;const retryTrackReport = () => {if (retries < maxRetries) {// 這里需要根據埋點庫具體邏輯實現重試上報// 假設埋點庫有一個重新上報的方法 retryReportif (trackInstance.retryReport) {trackInstance.retryReport();}retries++;} else {this.retryReport({type: errorType,message: `${errorType} 埋點上報錯誤: ${error.message}`,stack: error.stack || null});}};retryTrackReport();};}// 監控 Vue 錯誤monitorVueErrors() {if (typeof window.Vue !== 'undefined') {window.Vue.config.errorHandler = (err, vm, info) => {this.retryReport({type: 'vue',message: err.message,stack: err.stack,info: info});};}}// 監控 React 錯誤monitorReactErrors() {if (typeof window.React !== 'undefined' && typeof window.ReactDOM !== 'undefined') {const sdk = this;const { useErrorBoundary } = window.React;const ErrorBoundary = ({ children }) => {const { error, resetErrorBoundary } = useErrorBoundary({onError: (error, errorInfo) => {sdk.retryReport({type: 'react',message: error.message,stack: error.stack,info: errorInfo.componentStack});}});if (error) {return window.React.createElement('div', null, 'Something went wrong.');}return children;};// 可以考慮在這里將 ErrorBoundary 包裹在根組件上// 假設根組件是 RootComponentconst originalRender = window.ReactDOM.render;window.ReactDOM.render = function (element, container, callback) {const errorBoundaryWrappedElement = window.React.createElement(ErrorBoundary, null, element);return originalRender.call(this, errorBoundaryWrappedElement, container, callback);};}}// 上報錯誤reportError(errorData) {const xhr = new XMLHttpRequest();xhr.open('POST', this.options.reportUrl, true);xhr.setRequestHeader('Content-Type', 'application/json');xhr.onreadystatechange = () => {if (xhr.readyState === 4) {if (xhr.status === 200) {console.log('錯誤上報成功');} else {console.error('錯誤上報失敗');}}};xhr.send(JSON.stringify(errorData));}// 重試上報錯誤retryReport(errorData) {const maxRetries = 3;let retries = 0;const sendReport = () => {const xhr = new XMLHttpRequest();xhr.open('POST', this.options.reportUrl, true);xhr.setRequestHeader('Content-Type', 'application/json');xhr.onreadystatechange = () => {if (xhr.readyState === 4) {if (xhr.status === 200) {console.log('錯誤上報成功');} else {if (retries < maxRetries) {retries++;sendReport();} else {console.error('錯誤上報失敗,達到最大重試次數');}}}};xhr.send(JSON.stringify(errorData));};sendReport();}
}// 【使用示例】
// 假設已經引入了 @company/example-tracking 庫(業務埋點庫)
import Tracking from '@company/example-tracking';const sdk = new FrontendMonitoringSDK({// 錯誤上報接口地址reportUrl: 'https://your-report-url.com',// 業務埋點track: {Track: Tracking,config: {enable: true,// 業務埋點上報地址domain: 'https://test-maidian.company.cn',mdParams: {cv: new URLSearchParams(window.location.search).get('cv'),md_etype: 'h5log',},},errorType: 'Tracking'}
});
- 【模擬報錯】
// 模擬業務埋點庫
class MockTracking {constructor(config) {this.config = config;}// 模擬上報方法report() {try {// 模擬上報失敗throw new Error('埋點上報失敗');} catch (error) {if (this.onError) {this.onError(error);}}}// 模擬重試上報方法retryReport() {this.report();}// 定義 onError 方法onError(error) {console.log('MockTracking 捕獲到錯誤:', error.message);// 可以在這里添加更多的錯誤處理邏輯}
}// 初始化 SDK
const sdk = new FrontendMonitoringSDK({// 錯誤上報接口地址reportUrl: 'https://your-report-url.com',// 業務埋點track: {Track: MockTracking,config: {enable: true,// 業務埋點上報地址domain: 'https://test-maidian.company.cn',mdParams: {cv: new URLSearchParams(window.location.search).get('cv'),md_etype: 'h5log',},},errorType: 'Tracking'}
});// 1. 模擬 Promise 的錯誤
const promiseError = new Promise((_, reject) => {reject(new Error('Promise 錯誤'));
});// 2. 模擬接口的報錯 -- 使用 axios 請求
import axios from 'axios';
// 模擬一個不存在的接口地址
const apiError = axios.get('https://nonexistent-api-url.com');// 3. 模擬資源的報錯
const script = document.createElement('script');
script.src = 'https://nonexistent-script-url.js';
document.body.appendChild(script);// 4. 模擬埋點上報報錯
const trackInstance = new MockTracking({enable: true,domain: 'https://test-maidian.company.cn',mdParams: {cv: new URLSearchParams(window.location.search).get('cv'),md_etype: 'h5log',},
});
// 業務代碼調用時無需再寫 try...catch
trackInstance.report();