import { uploadErrorLog } from "./uploadLog"; import state from "/src/state"; import { getQuery } from "/src/utils/queryString"; type uploadType = { clientType?: string; phone?: string | undefined | null; userAgent?: string; appType?: string; content?: string; exceptionType?: string; exceptionTime?: string; deviceType?: string | null; deviceVersion?: string | null } const query: any = getQuery(); /** * 页面有报错时上传错误日志 * @params */ export default function useErrorLog() { const _uploadErrorLog = async (event: any) => { // 错误信息 const contentError = `Error message: ${event.target.tagName || ''};${ event.target.src || event.target.href || '' };lineno: ${event.lineno || ''};message: ${ event.message || '' };filename: ${event.filename || ''};fileUrl: ${ window.location.href };reason: ${event.reason?.message || ''}; stack: ${event.reason?.stack || ''}; bizId: ${state.examSongId || query.id || ''}; partIndex: ${query["part-index"] || state.partIndex || 0}`; uploadErrorLog(contentError) }; /** * 开始监听错误日志并上传 */ const startListenErrorLog = () => { console.log('mount useErrorLog'); window.addEventListener('error', _uploadErrorLog); window.addEventListener('unhandledrejection', _uploadErrorLog); }; /** * 停止监听 */ const stopListenErrorLog = () => { window.removeEventListener('error', _uploadErrorLog); window.removeEventListener('unhandledrejection', _uploadErrorLog); }; return { startListenErrorLog, stopListenErrorLog }; }