Skip to content

Commit b4172b0

Browse files
committed
chore: add error metrics for unhandles exeptions and rejections
1 parent d86b108 commit b4172b0

4 files changed

Lines changed: 22 additions & 5 deletions

File tree

src/main.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,39 @@ window.scriptObserver = new MutationObserver(callback);
129129
// Start observing the target node for configured mutations
130130
window.scriptObserver.observe(mainScripts, config);
131131

132+
let Metrics = null;
133+
132134
window.onerror = function (msg, url, line, ...err) {
133135
console.error("Caught Critical error from: " + url + ":" + line + " message: " + msg, ...err);
136+
if(Metrics) {
137+
Metrics.countEvent(Metrics.EVENT_TYPE.ERROR, "uncaught", "main.js");
138+
}
134139
return true; // same as preventDefault
135140
};
136141

142+
window.addEventListener("unhandledrejection", function (event){
143+
console.error("Caught unhandledrejection from: ", event);
144+
if(Metrics) {
145+
Metrics.countEvent(Metrics.EVENT_TYPE.ERROR, "unhandled", "rejection");
146+
}
147+
return true; // same as preventDefault
148+
});
149+
137150
define(function (require) {
138151

139152

140153
// Load compatibility shims--these need to load early, be careful moving this
141154
// Event dispatcher must be loaded before worker comm https://github.com/phcode-dev/phoenix/pull/678
142-
require(["utils/Compatibility", "utils/EventDispatcher"], function () {
155+
require(["utils/Metrics", "utils/Compatibility", "utils/EventDispatcher"], function () {
156+
Metrics = require("utils/Metrics");
143157
// Load the brackets module. This is a self-running module that loads and runs the entire application.
144158
try{
145159
require(["brackets"]);
146160
} catch (e) {
147161
console.error('Critical error when loading brackets. Trying to reload again.');
162+
if(Metrics) {
163+
Metrics.countEvent(Metrics.EVENT_TYPE.ERROR, "loadErr", "reload");
164+
}
148165
window.location.reload();
149166
}
150167
});

src/utils/AppInit.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* loading.
3333
*/
3434
define(function (require, exports, module) {
35-
35+
const Metrics = require("utils/Metrics");
3636

3737
/*
3838
* Fires when the base htmlContent/main-view.html is loaded
@@ -88,6 +88,7 @@ define(function (require, exports, module) {
8888
} catch (e) {
8989
console.error("Exception when calling a 'brackets done loading' handler: " + e);
9090
console.log(e.stack);
91+
Metrics.countEvent(Metrics.EVENT_TYPE.ERROR, "appInit", "doneLoading");
9192
}
9293
}
9394

src/utils/Metrics.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ define(function (require, exports, module) {
7878
SHARING: "sharing",
7979
PERFORMANCE: "performance",
8080
STORAGE: "storage",
81-
NEW_PROJECT: "new-project"
81+
NEW_PROJECT: "new-project",
82+
ERROR: "error"
8283
};
8384

8485
/**

test/spec/Metrics-test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ define(function (require, exports, module) {
3838
let data = Metrics.getLoggedDataForAudit();
3939
expect(data.get("typec.cat.sub")).toEqual({ eventType: 'count', sum: 2, count: 2 });
4040
expect(data.get("typev.cat.sub")).toEqual({ eventType: 'val', sum: -10, count: 2 });
41-
expect(data.size).toEqual(2);
4241
});
4342

4443
it("should log health metrics For audit even if disabled", function () {
@@ -53,7 +52,6 @@ define(function (require, exports, module) {
5352
data = Metrics.getLoggedDataForAudit();
5453
expect(data.get("typec.cat.sub")).toEqual({ eventType: 'count', sum: 2, count: 2 });
5554
expect(data.get("typev.cat.sub")).toEqual({ eventType: 'val', sum: -10, count: 2 });
56-
expect(data.size).toEqual(2);
5755
});
5856

5957
it("should delete 1000 entries if 3000 max entries have been logged", function () {

0 commit comments

Comments
 (0)