Skip to content

Commit c2ba7d2

Browse files
committed
move error handling from utils to app
1 parent 3c514df commit c2ba7d2

2 files changed

Lines changed: 71 additions & 79 deletions

File tree

js/app.js

Lines changed: 65 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -183,80 +183,87 @@ function App () {
183183
* @returns {Promise<object>} the config used
184184
*/
185185
this.start = async function () {
186-
const configObj = Utils.loadConfig();
187-
global.config = configObj.fullConf;
188-
const config = global.config;
189-
Utils.checkConfigFile(configObj);
190-
191-
global.defaultModulesDir = config.defaultModulesDir;
192-
defaultModules = require(`${global.root_path}/${global.defaultModulesDir}/defaultmodules`);
193-
194-
Log.setLogLevel(config.logLevel);
195-
196-
env = getEnvVarsAsObj();
197-
// check for deprecated css/custom.css and move it to new location
198-
if ((!fs.existsSync(`${global.root_path}/${env.customCss}`)) && (fs.existsSync(`${global.root_path}/css/custom.css`))) {
199-
try {
200-
fs.renameSync(`${global.root_path}/css/custom.css`, `${global.root_path}/${env.customCss}`);
201-
Log.warn(`WARNING! Your custom css file was moved from ${global.root_path}/css/custom.css to ${global.root_path}/${env.customCss}`);
202-
} catch {
203-
Log.warn("WARNING! Your custom css file is currently located in the css folder. Please move it to the config folder!");
186+
try {
187+
const configObj = Utils.loadConfig();
188+
global.config = configObj.fullConf;
189+
const config = global.config;
190+
Utils.checkConfigFile(configObj);
191+
192+
global.defaultModulesDir = config.defaultModulesDir;
193+
defaultModules = require(`${global.root_path}/${global.defaultModulesDir}/defaultmodules`);
194+
195+
Log.setLogLevel(config.logLevel);
196+
197+
env = getEnvVarsAsObj();
198+
// check for deprecated css/custom.css and move it to new location
199+
if ((!fs.existsSync(`${global.root_path}/${env.customCss}`)) && (fs.existsSync(`${global.root_path}/css/custom.css`))) {
200+
try {
201+
fs.renameSync(`${global.root_path}/css/custom.css`, `${global.root_path}/${env.customCss}`);
202+
Log.warn(`WARNING! Your custom css file was moved from ${global.root_path}/css/custom.css to ${global.root_path}/${env.customCss}`);
203+
} catch {
204+
Log.warn("WARNING! Your custom css file is currently located in the css folder. Please move it to the config folder!");
205+
}
204206
}
205-
}
206207

207-
// get the used module positions
208-
Utils.getModulePositions();
209-
210-
let modules = [];
211-
for (const module of config.modules) {
212-
if (module.disabled) continue;
213-
if (module.module) {
214-
if (Utils.moduleHasValidPosition(module.position) || typeof (module.position) === "undefined") {
215-
// Only add this module to be loaded if it is not a duplicate (repeated instance of the same module)
216-
if (!modules.includes(module.module)) {
217-
modules.push(module.module);
208+
// get the used module positions
209+
Utils.getModulePositions();
210+
211+
let modules = [];
212+
for (const module of config.modules) {
213+
if (module.disabled) continue;
214+
if (module.module) {
215+
if (Utils.moduleHasValidPosition(module.position) || typeof (module.position) === "undefined") {
216+
// Only add this module to be loaded if it is not a duplicate (repeated instance of the same module)
217+
if (!modules.includes(module.module)) {
218+
modules.push(module.module);
219+
}
220+
} else {
221+
Log.warn("Invalid module position found for this configuration:" + `\n${JSON.stringify(module, null, 2)}`);
218222
}
219223
} else {
220-
Log.warn("Invalid module position found for this configuration:" + `\n${JSON.stringify(module, null, 2)}`);
224+
Log.warn("No module name found for this configuration:" + `\n${JSON.stringify(module, null, 2)}`);
221225
}
222-
} else {
223-
Log.warn("No module name found for this configuration:" + `\n${JSON.stringify(module, null, 2)}`);
224226
}
225-
}
226227

227-
setGlobalDispatcher(new Agent({ connect: { timeout: fetch_timeout } }));
228+
setGlobalDispatcher(new Agent({ connect: { timeout: fetch_timeout } }));
228229

229-
await loadModules(modules);
230+
await loadModules(modules);
230231

231-
httpServer = new Server(configObj);
232-
const { app, io } = await httpServer.open();
233-
Log.log("Server started ...");
232+
httpServer = new Server(configObj);
233+
const { app, io } = await httpServer.open();
234+
Log.log("Server started ...");
234235

235-
const nodePromises = [];
236-
for (let nodeHelper of nodeHelpers) {
237-
nodeHelper.setExpressApp(app);
238-
nodeHelper.setSocketIO(io);
236+
const nodePromises = [];
237+
for (let nodeHelper of nodeHelpers) {
238+
nodeHelper.setExpressApp(app);
239+
nodeHelper.setSocketIO(io);
239240

240-
try {
241-
nodePromises.push(nodeHelper.start());
242-
} catch (error) {
243-
Log.error(`Error when starting node_helper for module ${nodeHelper.name}:`);
244-
Log.error(error);
241+
try {
242+
nodePromises.push(nodeHelper.start());
243+
} catch (error) {
244+
Log.error(`Error when starting node_helper for module ${nodeHelper.name}:`);
245+
Log.error(error);
246+
}
245247
}
246-
}
247248

248-
const results = await Promise.allSettled(nodePromises);
249+
const results = await Promise.allSettled(nodePromises);
249250

250-
// Log errors that happened during async node_helper startup
251-
results.forEach((result) => {
252-
if (result.status === "rejected") {
253-
Log.error(result.reason);
254-
}
255-
});
251+
// Log errors that happened during async node_helper startup
252+
results.forEach((result) => {
253+
if (result.status === "rejected") {
254+
Log.error(result.reason);
255+
}
256+
});
256257

257-
Log.log("Sockets connected & modules started ...");
258+
Log.log("Sockets connected & modules started ...");
258259

259-
return global.config;
260+
return global.config;
261+
} catch {
262+
const int32 = new Int32Array(new SharedArrayBuffer(4));
263+
// wait 1000ms before exiting so that child processes (e.g. systeminformation) have some additional time
264+
Atomics.wait(int32, 0, 0, 1000);
265+
process.exit(1);
266+
}
260267
};
261268

262269
/**
@@ -328,20 +335,6 @@ function App () {
328335
await this.stop();
329336
process.exit(0);
330337
});
331-
332-
/**
333-
*
334-
* @param {number} ms milliseconds to wait
335-
*/
336-
function blockingSleep (ms) {
337-
const int32 = new Int32Array(new SharedArrayBuffer(4));
338-
Atomics.wait(int32, 0, 0, ms);
339-
}
340-
341-
process.on("exit", () => {
342-
// wait before exiting so that child processes (e.g. systeminformation) have some additional time
343-
blockingSleep(1000);
344-
});
345338
}
346339

347340
module.exports = new App();

js/utils.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ const loadConfig = () => {
172172
} else {
173173
Log.error(`Cannot access config file: ${configFilename}\n${error.message}`);
174174
}
175-
process.exit(1);
175+
throw new Error("process.exit:1", { cause: error });
176176
}
177-
return {};
178177
};
179178

180179
/**
@@ -220,7 +219,7 @@ const checkConfigFile = (configObject) => {
220219
errorMessage += `\nLine ${error.line} column ${error.column}: ${error.message}`;
221220
}
222221
Log.error(errorMessage);
223-
process.exit(1);
222+
throw new Error("process.exit:1");
224223
}
225224
};
226225

@@ -242,27 +241,27 @@ const validateModulePositions = (data) => {
242241
// `modules` always exists (defaults.js provides a default array), but guard against it being overridden with a non-array value
243242
if (data.modules !== undefined && !Array.isArray(data.modules)) {
244243
Log.error("This module configuration contains errors:\nmodules must be an array");
245-
process.exit(1);
244+
throw new Error("process.exit:1");
246245
}
247246

248247
// Validate each module entry
249248
for (const [index, mod] of (data.modules ?? []).entries()) {
250249
// Each module entry must be an object so we can safely inspect its fields
251250
if (mod === null || typeof mod !== "object" || Array.isArray(mod)) {
252251
Log.error(`This module configuration contains errors:\n${JSON.stringify(mod, null, 2)}\nmodule entry must be an object`);
253-
process.exit(1);
252+
throw new Error("process.exit:1");
254253
}
255254

256255
// `module` (the module name) is required and must be a string
257256
if (typeof mod.module !== "string") {
258257
Log.error(`This module configuration contains errors:\n${JSON.stringify(mod, null, 2)}\nmodule: must be a string`);
259-
process.exit(1);
258+
throw new Error("process.exit:1");
260259
}
261260

262261
// `position` is optional, but must be a string when provided
263262
if (mod.position !== undefined && typeof mod.position !== "string") {
264263
Log.error(`This module configuration contains errors:\n${JSON.stringify(mod, null, 2)}\nposition: must be a string`);
265-
process.exit(1);
264+
throw new Error("process.exit:1");
266265
}
267266

268267
// `position` is optional, but when set it must match a known region

0 commit comments

Comments
 (0)