-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathindex.js
More file actions
128 lines (113 loc) · 5.57 KB
/
index.js
File metadata and controls
128 lines (113 loc) · 5.57 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* eslint-disable no-console */
import 'dotenv/config';
import fs from 'fs-extra';
import OTNode from './ot-node.js';
import { NODE_ENVIRONMENTS } from './src/constants/constants.js';
process.env.NODE_ENV =
process.env.NODE_ENV && Object.values(NODE_ENVIRONMENTS).includes(process.env.NODE_ENV)
? process.env.NODE_ENV
: NODE_ENVIRONMENTS.DEVELOPMENT;
(async () => {
let userConfig = null;
try {
if (process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT && process.argv.length === 3) {
const configurationFilename = process.argv[2];
userConfig = JSON.parse(await fs.promises.readFile(process.argv[2]));
userConfig.configFilename = configurationFilename;
}
} catch (error) {
console.log('Unable to read user configuration from file: ', process.argv[2]);
process.exit(1);
}
try {
const node = new OTNode(userConfig);
await node.start();
} catch (e) {
console.error(`Error occurred while start ot-node, error message: ${e}. ${e.stack}`);
// console.error(`Trying to recover from older version`);
// if (process.env.NODE_ENV !== NODE_ENVIRONMENTS.DEVELOPMENT) {
// const rootPath = path.join(appRootPath.path, '..');
// const oldVersionsDirs = (await fs.promises.readdir(rootPath, { withFileTypes: true }))
// .filter((dirent) => dirent.isDirectory())
// .map((dirent) => dirent.name)
// .filter((name) => semver.valid(name) && !appRootPath.path.includes(name));
//
// if (oldVersionsDirs.length === 0) {
// console.error(
// `Failed to start OT-Node, no backup code available. Error message: ${e.message}`,
// );
// process.exit(1);
// }
//
// const oldVersion = oldVersionsDirs.sort(semver.compare).pop();
// const oldversionPath = path.join(rootPath, oldVersion);
// execSync(`ln -sfn ${oldversionPath} ${rootPath}/current`);
// await fs.promises.rm(appRootPath.path, { force: true, recursive: true });
// }
process.exit(1);
}
})();
process.on('unhandledRejection', (err) => {
// Handle specific libp2p peer lookup failures that escape try-catch blocks
if (err && err.code === 'ERR_LOOKUP_FAILED') {
console.warn(`Peer lookup failed (ERR_LOOKUP_FAILED): ${err.message}`);
return; // Don't crash for peer lookup failures
}
// Handle ECONNRESET errors gracefully - these are common network issues
if (err && (err.code === 'ECONNRESET' || err.errno === -104)) {
console.warn(`Network connection reset (ECONNRESET): ${err.message}`);
return; // Don't crash for connection reset errors
}
// Handle ERR_UNSUPPORTED_PROTOCOL errors gracefully
if (err && err.code === 'ERR_UNSUPPORTED_PROTOCOL') {
console.warn(`Unsupported protocol error (ERR_UNSUPPORTED_PROTOCOL): ${err.message}`);
return; // Don't crash for protocol errors
}
// Handle EPIPE (broken pipe) errors gracefully
if (err && (err.code === 'EPIPE' || err.errno === -32)) {
console.warn(`Broken pipe error (EPIPE): ${err.message}`);
return; // Don't crash for broken pipe errors
}
// Handle ETIMEDOUT errors gracefully - these are common database connection timeouts
if (err && (err.code === 'ETIMEDOUT' || err.errno === -110)) {
console.warn(`Connection timeout error (ETIMEDOUT): ${err.message}`);
return; // Don't crash for timeout errors
}
// Handle Sequelize "Got timeout reading communication packets" errors gracefully
if (err && err.message && err.message.includes('Got timeout reading communication packets')) {
console.warn(`Sequelize communication timeout error: ${err.message}`);
return; // Don't crash for database communication timeout errors
}
// For all other unhandled rejections, crash the node
console.error('Something went really wrong! OT-node shutting down...', err);
process.exit(1);
});
process.on('uncaughtException', (err) => {
// Handle ERR_UNSUPPORTED_PROTOCOL errors gracefully
if (err && err.code === 'ERR_UNSUPPORTED_PROTOCOL') {
console.warn(`Unsupported protocol error (ERR_UNSUPPORTED_PROTOCOL): ${err.message}`);
return; // Don't crash for protocol errors
}
// Handle EPIPE (broken pipe) errors gracefully
if (err && (err.code === 'EPIPE' || err.errno === -32)) {
console.warn(`Broken pipe error (EPIPE): ${err.message}`);
return; // Don't crash for broken pipe errors
}
// Handle ECONNRESET errors gracefully
if (err && (err.code === 'ECONNRESET' || err.errno === -104)) {
console.warn(`Network connection reset (ECONNRESET): ${err.message}`);
return; // Don't crash for connection reset errors
}
// Handle ETIMEDOUT errors gracefully - these are common database connection timeouts
if (err && (err.code === 'ETIMEDOUT' || err.errno === -110)) {
console.warn(`Connection timeout error (ETIMEDOUT): ${err.message}`);
return; // Don't crash for timeout errors
}
// Handle Sequelize "Got timeout reading communication packets" errors gracefully
if (err && err.message && err.message.includes('Got timeout reading communication packets')) {
console.warn(`Sequelize communication timeout error: ${err.message}`);
return; // Don't crash for database communication timeout errors
}
console.error('Something went really wrong! OT-node shutting down...', err);
process.exit(1);
});