You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2020/08/26 02:53:50 UTC
[skywalking-client-js] 30/48: feat: trace resource error
This is an automated email from the ASF dual-hosted git repository.
wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-client-js.git
commit eef867b55a4a6e1be2c16a181ec860ef0fd7d275
Author: Qiuxia Fan <fi...@outlook.com>
AuthorDate: Wed Jan 8 15:17:28 2020 +0800
feat: trace resource error
---
src/errors/index.ts | 3 ++-
src/errors/resource.ts | 47 +++++++++++++++++++++++++++++++++++++++++++++++
src/monitor.ts | 3 ++-
src/services/base.ts | 4 +++-
4 files changed, 54 insertions(+), 3 deletions(-)
diff --git a/src/errors/index.ts b/src/errors/index.ts
index b213fd0..8a10f4c 100644
--- a/src/errors/index.ts
+++ b/src/errors/index.ts
@@ -17,7 +17,8 @@
import JSErrors from './js';
import PromiseErrors from './promise';
import AjaxErrors from './ajax';
+import ResourceErrors from './resource';
export {
- JSErrors, PromiseErrors, AjaxErrors,
+ JSErrors, PromiseErrors, AjaxErrors, ResourceErrors,
};
diff --git a/src/errors/resource.ts b/src/errors/resource.ts
new file mode 100644
index 0000000..c861357
--- /dev/null
+++ b/src/errors/resource.ts
@@ -0,0 +1,47 @@
+/**
+ * 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 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
+ 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();
+ });
+ }
+}
+export default new ResourceErrors();
diff --git a/src/monitor.ts b/src/monitor.ts
index a164606..934b711 100644
--- a/src/monitor.ts
+++ b/src/monitor.ts
@@ -16,7 +16,7 @@
*/
import { CustomOptionsType } from './types';
-import { JSErrors, PromiseErrors, AjaxErrors } from './errors/index';
+import { JSErrors, PromiseErrors, AjaxErrors, ResourceErrors } from './errors/index';
const ClientMonitor = {
customOptions: {
@@ -43,6 +43,7 @@ const ClientMonitor = {
}
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;
diff --git a/src/services/base.ts b/src/services/base.ts
index fa655c9..08fa270 100644
--- a/src/services/base.ts
+++ b/src/services/base.ts
@@ -20,7 +20,9 @@ import { errorInfoFeilds } from './types';
export default class Base {
public reportUrl: string;
+
public serviceName: string;
+
public logInfo: errorInfoFeilds = {
category: ErrorsCategory.UNKNOW_ERROR,
grade: GradeTypeEnum.INFO,
@@ -52,7 +54,7 @@ export default class Base {
Task.addTask(this.reportUrl, errorInfo);
} catch (error) {
- // console.log(error);
+ throw error;
}
}