index.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { uploadErrorLog } from "./uploadLog";
  2. import state from "/src/state";
  3. import { getQuery } from "/src/utils/queryString";
  4. type uploadType = {
  5. clientType?: string;
  6. phone?: string | undefined | null;
  7. userAgent?: string;
  8. appType?: string;
  9. content?: string;
  10. exceptionType?: string;
  11. exceptionTime?: string;
  12. deviceType?: string | null;
  13. deviceVersion?: string | null
  14. }
  15. const query: any = getQuery();
  16. /**
  17. * 页面有报错时上传错误日志
  18. * @params
  19. */
  20. export default function useErrorLog() {
  21. const _uploadErrorLog = async (event: any) => {
  22. // 错误信息
  23. const contentError = `Error message: ${event.target.tagName || ''};${
  24. event.target.src || event.target.href || ''
  25. };lineno: ${event.lineno || ''};message: ${
  26. event.message || ''
  27. };filename: ${event.filename || ''};fileUrl: ${
  28. window.location.href
  29. };reason: ${event.reason?.message || ''};
  30. stack: ${event.reason?.stack || ''};
  31. bizId: ${state.examSongId || query.id || ''};
  32. partIndex: ${query["part-index"] || state.partIndex || 0}`;
  33. uploadErrorLog(contentError)
  34. };
  35. /**
  36. * 开始监听错误日志并上传
  37. */
  38. const startListenErrorLog = () => {
  39. console.log('mount useErrorLog');
  40. window.addEventListener('error', _uploadErrorLog);
  41. window.addEventListener('unhandledrejection', _uploadErrorLog);
  42. };
  43. /**
  44. * 停止监听
  45. */
  46. const stopListenErrorLog = () => {
  47. window.removeEventListener('error', _uploadErrorLog);
  48. window.removeEventListener('unhandledrejection', _uploadErrorLog);
  49. };
  50. return {
  51. startListenErrorLog,
  52. stopListenErrorLog
  53. };
  54. }