feat: trace vue error
diff --git a/src/errors/index.ts b/src/errors/index.ts
index 8a10f4c..180a7ba 100644
--- a/src/errors/index.ts
+++ b/src/errors/index.ts
@@ -14,11 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
import JSErrors from './js';
import PromiseErrors from './promise';
import AjaxErrors from './ajax';
import ResourceErrors from './resource';
+import VueErrors from './vue';
export {
- JSErrors, PromiseErrors, AjaxErrors, ResourceErrors,
+ JSErrors, PromiseErrors, AjaxErrors, ResourceErrors, VueErrors,
};
diff --git a/src/errors/resource.ts b/src/errors/resource.ts
index c861357..0db5364 100644
--- a/src/errors/resource.ts
+++ b/src/errors/resource.ts
@@ -21,26 +21,30 @@
class ResourceErrors extends Base {
public handleErrors(options: {reportUrl: string; serviceName: string}) {
window.addEventListener('error', (event) => {
- if (!event) {
- return;
- }
- this.reportUrl = options.reportUrl;
- this.serviceName = options.serviceName;
- const target: any = event.target || event.srcElement;
- const isElementTarget = target instanceof HTMLScriptElement
- || target instanceof HTMLLinkElement || target instanceof HTMLImageElement;
-
- if (!isElementTarget) { // return js error
+ try {
+ if (!event) {
return;
+ }
+ this.reportUrl = options.reportUrl;
+ this.serviceName = options.serviceName;
+ const target: any = event.target || event.srcElement;
+ const isElementTarget = target instanceof HTMLScriptElement
+ || target instanceof HTMLLinkElement || target instanceof HTMLImageElement;
+
+ if (!isElementTarget) { // return js error
+ return;
+ }
+ this.logInfo = {
+ category: ErrorsCategory.RESOURCE_ERROR,
+ grade: target.tagName === 'IMG' ? GradeTypeEnum.WARNING : GradeTypeEnum.ERROR,
+ errorUrl: target.src || target.href,
+ errorInfo: target,
+ message: `load ${target.tagName} resource error`,
+ };
+ this.traceInfo();
+ } catch (error) {
+ throw error;
}
- this.logInfo = {
- category: ErrorsCategory.RESOURCE_ERROR,
- grade: target.tagName === 'IMG' ? GradeTypeEnum.WARNING : GradeTypeEnum.ERROR,
- errorUrl: target.src || target.href,
- errorInfo: target,
- message: `load ${target.tagName} resource error`,
- };
- this.traceInfo();
});
}
}
diff --git a/src/errors/vue.ts b/src/errors/vue.ts
new file mode 100644
index 0000000..76be92c
--- /dev/null
+++ b/src/errors/vue.ts
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import Base from '../services/base';
+import { GradeTypeEnum, ErrorsCategory } from '../services/constant';
+
+class VueErrors extends Base {
+ public handleErrors(options: {reportUrl: string; serviceName: string}, Vue: any) {
+ Vue.config.errorHandler = (error: Error, vm: any, info: string) => {
+ console.log(error);
+ try {
+ this.reportUrl = options.reportUrl;
+ this.serviceName = options.serviceName;
+ this.logInfo = {
+ category: ErrorsCategory.VUE_ERROR,
+ grade: GradeTypeEnum.ERROR,
+ errorUrl: '',
+ errorInfo: error,
+ message: info,
+ };
+ this.traceInfo();
+ } catch (error) {
+ throw error;
+ }
+ };
+ }
+}
+
+export default new VueErrors();
diff --git a/src/monitor.ts b/src/monitor.ts
index 934b711..f912ee8 100644
--- a/src/monitor.ts
+++ b/src/monitor.ts
@@ -16,7 +16,7 @@
*/
import { CustomOptionsType } from './types';
-import { JSErrors, PromiseErrors, AjaxErrors, ResourceErrors } from './errors/index';
+import { JSErrors, PromiseErrors, AjaxErrors, ResourceErrors, VueErrors } from './errors/index';
const ClientMonitor = {
customOptions: {
@@ -24,7 +24,6 @@
promiseErrors: true,
consoleErrors: false,
vueErrors: false,
- reactErrors: false,
ajaxErrors: true,
resourceErrors: true,
} as CustomOptionsType,
@@ -32,23 +31,26 @@
register(options: CustomOptionsType) {
const { serviceName, reportUrl } = options;
- this.customOptions = options;
+ this.customOptions = {
+ ...this.customOptions,
+ ...options,
+ };
+
if (this.customOptions.jsErrors) {
- this.customOptions.jsErrors = options.jsErrors;
JSErrors.handleErrors({reportUrl, serviceName});
}
if (this.customOptions.promiseErrors) {
- this.customOptions.promiseErrors = options.promiseErrors || this.customOptions.promiseErrors;
PromiseErrors.handleErrors({reportUrl, serviceName});
}
if (this.customOptions.resourceErrors) {
- this.customOptions.resourceErrors = options.resourceErrors;
ResourceErrors.handleErrors({reportUrl, serviceName});
}
if (this.customOptions.ajaxErrors) {
- this.customOptions.ajaxErrors = options.ajaxErrors || this.customOptions.ajaxErrors;
AjaxErrors.handleError({reportUrl, serviceName});
}
+ if (this.customOptions.vueErrors && this.customOptions.vue) {
+ VueErrors.handleErrors({reportUrl, serviceName}, this.customOptions.vue);
+ }
},
};
diff --git a/src/services/base.ts b/src/services/base.ts
index 08fa270..d7f3cea 100644
--- a/src/services/base.ts
+++ b/src/services/base.ts
@@ -62,15 +62,15 @@
let message = `error category:${this.logInfo.category}\r\n log info:${this.logInfo.message}\r\n
error url: ${this.logInfo.errorUrl}\r\n `;
switch (this.logInfo.category) {
- case ErrorsCategory.JS_ERROR:
- message += `error line number: ${this.logInfo.line}\r\n error col number:${this.logInfo.col}\r\n`;
- if (this.logInfo.errorInfo && this.logInfo.errorInfo.stack) {
- message += `error stack: ${this.logInfo.errorInfo.stack}\r\n`;
- }
- break;
- default:
- message += `other error: ${this.logInfo.errorInfo}\r\n`;
- break;
+ case ErrorsCategory.JS_ERROR:
+ message += `error line number: ${this.logInfo.line}\r\n error col number:${this.logInfo.col}\r\n`;
+ if (this.logInfo.errorInfo && this.logInfo.errorInfo.stack) {
+ message += `error stack: ${this.logInfo.errorInfo.stack}\r\n`;
+ }
+ break;
+ default:
+ message += `other error: ${this.logInfo.errorInfo}\r\n`;
+ break;
}
const recordInfo = {
...this.logInfo,
diff --git a/src/types.d.ts b/src/types.d.ts
index e916ee6..096bfe4 100644
--- a/src/types.d.ts
+++ b/src/types.d.ts
@@ -22,7 +22,6 @@
promiseErrors: boolean;
consoleErrors: boolean;
vueErrors: boolean;
- reactErrors: boolean;
ajaxErrors: boolean;
resourceErrors: boolean;
}