Skip to content

Commit 4754220

Browse files
committed
Write a better console.warn function
1 parent 7ef3bf1 commit 4754220

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

src/default.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
apiUrlPrefix,
88
version,
99
defaultNamespace,
10-
sendError
10+
sendError,
11+
warn
1112
) {
1213
try {
1314
/////////////////////
@@ -74,8 +75,21 @@
7475
//
7576

7677
// A simple log function so the user knows why a request is not being send
77-
var warn = function (message) {
78-
if (con && con.warn) con.warn("Simple Analytics:", message);
78+
warn = function () {
79+
if (con && con.warn) {
80+
// 1. Convert args to a normal array
81+
var args = [].slice.call(arguments);
82+
83+
// 2. Prepend log prefix
84+
args.unshift("Simple Analytics:");
85+
86+
// 3. Pass along arguments to console.warn
87+
con.warn.apply(con, args);
88+
}
89+
};
90+
91+
var warnInFunction = function (name, error) {
92+
warn("Error in your " + name + " function:", error);
7993
};
8094

8195
var hasProp = function (obj, prop) {
@@ -445,7 +459,7 @@
445459
? Intl.DateTimeFormat().resolvedOptions().timeZone
446460
: undefinedVar;
447461
} catch (e) {
448-
/* Do nothing */
462+
warn(error);
449463
}
450464

451465
/////////////////////
@@ -637,6 +651,7 @@
637651
) * 5
638652
);
639653
} catch (error) {
654+
warn(error);
640655
return 0;
641656
}
642657
};
@@ -763,7 +778,7 @@
763778
try {
764779
performaceEntryType = perf.getEntriesByType(navigationText)[0].type;
765780
} catch (error) {
766-
// Do nothing
781+
warn(error);
767782
}
768783

769784
var userNavigated = performaceEntryType

0 commit comments

Comments
 (0)