ubugtrack-crash-report
Version:
Catch all exception and error then report it on your ubugtrack account
122 lines (101 loc) • 2.75 kB
JavaScript
/********************************************************************************
* ubugtrack.com - Crash Report JS V1
* Send App Error to uBugtrack crashes service
* Created by Guillaume
* ******************************************************************************
*/
const process = require('process');
const querystring = require('querystring');
const https = require('https');
var ubugtrackCrashKey = null;
var ubugtrackVersion = null;
module.exports = class ubugtrackCrashReport
{
/*
* Call this function to init the Crash Repporter
*/
static initCrashReporter(crashKey,version)
{
if (ubugtrackCrashKey == null)
{
ubugtrackCrashKey = crashKey;
ubugtrackVersion = version;
process.on('uncaughtException', (error) =>
{
var logtitle = error.message;
var logfull = error.message + "\n";
logfull += 'Stack trace:' + error.stack + "\n";
ubugtrackCrashReport.sendCrashDump(logtitle,logfull);
process.exit(1);
});
process.on('warning', (warning) =>
{
var logtitle = warning.name + ' : '+ warning.message;
var logfull = logtitle + "\n" + warning.message;
ubugtrackCrashReport.sendCrashDump(logtitle,logfull);
});
}
else
{
console.log('** uBugtrack already initiated');
}
}
/*
* Call this function to catch the express error
*/
static attachExpress(express)
{
if (ubugtrackCrashKey != null)
{
console.log('#UBUGTRACK Attach express');
express.use(function(error, req, res, next)
{
var logtitle = error.message;
var logfull = "Query: " + req.url + "\n\n";
logfull += error.message + "\n";
logfull += 'Stack trace:' + error.stack + "\n";
ubugtrackCrashReport.sendCrashDump(logtitle,logfull);
res.json({ message: error.message });
});
}
else
{
console.log('#UBUGTRACK NOT initiated');
}
}
/*
* Internal send Error function
*/
static sendCrashDump(logtitle,logfull)
{
if (ubugtrackCrashKey != null)
{
console.log('#UBUGTRACK ' + logtitle);
console.log('#UBUGTRACK ' + logfull);
var postData = querystring.stringify({
'project_key' : ubugtrackCrashKey,
'manufacturer': 'node.js',
'model': process.version,
'appversion' : ubugtrackVersion,
'logtitle' : logtitle,
'logfull' : logfull});
var postOptions = {
host: 'ubugtrack.com',
port: '443',
path: '/crashreport.add',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}};
var postReq = https.request(postOptions, function(res)
{
res.setEncoding('utf8');
res.on('data', function (chunk) {});
});
// post the data
postReq.write(postData);
postReq.end();
}
}
};