Created
December 19, 2018 13:38
-
-
Save fizerkhan/98ac4f42932ee5e1eb22bb524e0d8a47 to your computer and use it in GitHub Desktop.
Atatus Error Reporting in NetSuite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(["N/https"], function(https) { | |
function sendErrorPayload(apikey, error, options) { | |
if (!apikey) { | |
throw new Error("API Key is missing!") | |
} | |
if (!error || !error.stack) { | |
throw new Error("Invalid error!") | |
} | |
if (options.tags && !Object.prototype.toString.call(options.tags) === '[object Array]') { | |
throw new Error("Invalid tags!") | |
} | |
if (options.customData && !Object.prototype.toString.call(options.customData) === '[object Object]') { | |
throw new Error("Invalid custom data!") | |
} | |
// Default values if it does not exist | |
options.url = options.url || "http://localhost/test"; | |
options.ua = options.ua || "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"; | |
// Separate line and column numbers from a string of the form: (URI:Line:Column) | |
function extractLocation(urlLike) { | |
// Fail-fast but return locations like "(native)" | |
if (urlLike.indexOf(':') === -1) { | |
return [urlLike]; | |
} | |
var regExp = /(.+?)(?:\:(\d+))?(?:\:(\d+))?$/; | |
var parts = regExp.exec(urlLike.replace(/[\(\)]/g, '')); | |
return [parts[1], parts[2] || undefined, parts[3] || undefined]; | |
} | |
var STACK_REGEXP = /^\s*at .*(\S+\:\d+|\(native\))/m; | |
function parseErrorStackTrace(error) { | |
var filtered = error.stack.split('\n').filter(function(line) { | |
return !!line.match(STACK_REGEXP); | |
}, this); | |
return filtered.map(function(line) { | |
if (line.indexOf('(eval ') > -1) { | |
// Throw away eval information until we implement stacktrace.js/stackframe#8 | |
line = line.replace(/eval code/g, 'eval').replace(/(\(eval at [^\()]*)|(\)\,.*$)/g, ''); | |
} | |
var tokens = line.replace(/^\s+/, '').replace(/\(eval code/g, '(').split(/\s+/).slice(1); | |
var locationParts = extractLocation(tokens.pop()); | |
var functionName = tokens.join(' ') || undefined; | |
var fileName = ['eval', '<anonymous>'].indexOf(locationParts[0]) > -1 ? undefined : locationParts[0]; | |
return { | |
m: functionName || '[anonymous]', | |
f: fileName || 'anonymous', | |
ln: +locationParts[1], | |
cn: +locationParts[2], | |
}; | |
}, this); | |
} | |
var stack = parseErrorStackTrace(error); | |
var timestamp = new Date().getTime(); | |
var errorPayload = { | |
ts: timestamp, | |
apikey: apikey, | |
user: options.user || null, | |
tags: options.tags || null, | |
customData: options.customData || null, | |
errors: [{ | |
ts: timestamp, | |
class: "Error", | |
url: options.url, | |
message: error.message, | |
backTraces: stack, | |
breadCrumbs: [], | |
customData: null | |
}], | |
request: { | |
url: options.url, | |
ua: options.ua, | |
w: 1920, | |
h: 1080 | |
}, | |
aid: null, | |
sid: "", | |
v: options.version || "", | |
_v: "3.0.9", | |
} | |
var sURL = "https://br-rx.atatus.com/track/browser/errors?data=" + encodeURIComponent(JSON.stringify(errorPayload)); | |
var header = { | |
"Content-Length": "0", | |
"Content-Type": "image/gif" | |
}; | |
https.get({ | |
url: sURL, | |
headers: header | |
}); | |
} | |
// Sample error and options | |
var apikey = "YOUR_PROJECT_API_KEY"; | |
var error = new Error("atatus test error"); | |
var options = { | |
customData: { | |
plan: "premium", | |
beta_access: true | |
}, | |
user: { | |
id: "1", | |
email: "[email protected]", | |
name: "John Doe", | |
}, | |
tags: ["production", "premium"] | |
}; | |
sendErrorPayload(apikey, error, options); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment