File size: 1,622 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
const path = require( 'path' );
const { app } = require( 'electron' );
// eslint-disable-next-line no-restricted-modules
const request = require( 'superagent' );
const config = require( '../../lib/config' );
const log = require( '../../lib/logger' )( 'desktop:crash-tracker' );
const system = require( '../../lib/system' );

function finished( error, response, cb ) {
	if ( error ) {
		log.error( 'Failed to upload crash report', error );
	} else {
		log.info( 'Uploaded crash report' );
	}

	if ( typeof cb !== 'undefined' ) {
		cb( response );
	}
}

function gatherData( errorType, errorData ) {
	// Gather basic data
	return Object.assign( {}, system.getVersionData(), {
		time: parseInt( new Date().getTime() / 1000, 10 ),
		type: errorType,
		data: errorData,
	} );
}

module.exports = {
	isEnabled: function () {
		return config.crash_reporter.tracker;
	},

	track: function ( errorType, errorData, cb ) {
		if ( config.crash_reporter.tracker ) {
			// Send to crash tracker
			log.info( 'Sending crash report to ' + config.crash_reporter.url );

			request
				.post( config.crash_reporter.url )
				.send( gatherData( errorType, errorData ) )
				.end( function ( error, response ) {
					finished( error, response, cb );
				} );
		}
	},

	trackLog: function ( cb ) {
		const logFile = path.join( app.getPath( 'userData' ), config.debug.log_file );

		log.info( 'Uploading log file: ' + logFile );

		request
			.post( config.crash_reporter.url )
			.send( gatherData( 'logfile', logFile ) )
			.attach( 'log', logFile )
			.end( function ( error, response ) {
				finished( error, response, cb );
			} );
	},
};