File size: 1,649 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const { app, dialog } = require( 'electron' );
const crashTracker = require( '../../lib/crash-tracker' );
const log = require( '../../lib/logger' )( 'desktop:exceptions', {
	handleExceptions: true,
} );
const system = require( '../../lib/system' );

/**
 * Module variables
 */
let isReady = false;
let thereCanBeOnlyOne = false;

// We ignore any of these errors as they are probably temporary
const NETWORK_ERRORS = [ 'ETIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED', 'ECONNRESET' ];

function exit() {
	if ( isReady ) {
		app.quit();
	} else {
		process.exit( 1 ); // eslint-disable-line no-process-exit
	}
}

function showErrorAndExit( error ) {
	if ( thereCanBeOnlyOne ) {
		exit();
	}

	thereCanBeOnlyOne = true;

	dialog.showErrorBox(
		'WordPress.com ran into an error',
		'Please restart the app and try again.' +
			'\n\n' +
			'If you continue to have issues, please contact us at help@wordpress.com and mention the error details below:' +
			'\n\n' +
			error.stack +
			'\n\n' +
			'System info: ' +
			JSON.stringify( system.getVersionData() )
	);

	exit();
}

function isFatalError( error ) {
	return ! NETWORK_ERRORS.includes( error.code );
}

function exceptionHandler( error ) {
	if ( ! isFatalError( error ) ) {
		return;
	}

	log.error( error );

	if ( crashTracker.isEnabled() ) {
		crashTracker.track(
			'exception',
			{ name: error.name, message: error.message, stack: error.stack },
			function () {
				showErrorAndExit( error );
			}
		);
	} else {
		showErrorAndExit( error );
	}
}

module.exports = function () {
	app.on( 'ready', function () {
		isReady = true;
	} );

	process.on( 'uncaughtException', exceptionHandler );
};