-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnotifier.js
53 lines (49 loc) · 2.27 KB
/
notifier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
(function(window, location, undef) {
var root = location.protocol + "//" + location.host,
apiKey = window.AIRBRAKE_API_KEY,
env = window.AIRBRAKE_ENVIRONMENT || "production";
function xmlNode(nodeName, attributes, nodeValue) {
attributes = attributes ? " " + attributes : "";
return "<" + nodeName + attributes + ">" + nodeValue + "</" + nodeName + ">";
}
function escapeText(text) {
return (text + "").replace(/[&<>'"]/g, function(match) {
return "&#" + match.charCodeAt() + ";";
});
}
function getXML(message, file, line) {
file && (file = file.replace(root, "[PROJECT ROOT]"));
return '<?xml version="1.0" encoding="UTF-8"?>' +
xmlNode("notice", 'version="2.0"',
xmlNode("api-key", undef, apiKey) +
xmlNode("notifier", undef,
xmlNode("name", undef, "Airbrake Notifier") +
xmlNode("version", undef, "1.2.4") +
xmlNode("url", undef, "http://airbrake.io")
) +
xmlNode("error", undef,
xmlNode("class", undef, "Error") +
xmlNode("message", undef, escapeText(message)) +
(file && line && xmlNode("backtrace", undef, '<line method="" file="' + escapeText(file) + '" number="' + escapeText(line) + '" />'))
) +
xmlNode("request", undef,
xmlNode("component", undef, "frontend") +
xmlNode("action", undef, "javascript") +
xmlNode("url", undef, location.href) +
xmlNode("cgi-data", undef,
xmlNode("var", 'key="HTTP_USER_AGENT"', navigator.userAgent) +
xmlNode("var", 'key="HTTP_REFERER"', document.referrer)
)
) +
xmlNode("server-environment", undef,
xmlNode("project-root", undef, root) +
xmlNode("environment-name", undef, env)
)
);
}
window.onerror = (window.Airbrake = {}).notify = function(message, file, line) {
if (apiKey) {
new Image().src = "http://airbrake.io/notifier_api/v2/notices?data=" + encodeURIComponent(getXML(message, file, line));
}
};
})(this, location);