|
@@ -0,0 +1,53 @@
|
|
|
+import { uploadErrorLog } from "./uploadLog";
|
|
|
+
|
|
|
+type uploadType = {
|
|
|
+ clientType?: string;
|
|
|
+ phone?: string | undefined | null;
|
|
|
+ userAgent?: string;
|
|
|
+ appType?: string;
|
|
|
+ content?: string;
|
|
|
+ exceptionType?: string;
|
|
|
+ exceptionTime?: string;
|
|
|
+ deviceType?: string | null;
|
|
|
+ deviceVersion?: string | null
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * 页面有报错时上传错误日志
|
|
|
+ * @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 || ''};`;
|
|
|
+ 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
|
|
|
+ };
|
|
|
+}
|