XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

SpaceNinjaServer

A simple server for a small space ninja game

公开
关注 0 Fork 1 Star 0
返回提交历史

XFEstudio/SpaceNinjaServer

fix: limit scope of 'catch' for MongoDB connection errors (#4162)

Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/4162 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>

bb5666cc
Sainan <63328889+Sainan@users.noreply.github.com>
提交于

代码差异

1 个文件 +48 -47
Modified src/index.ts +48 -47
@@ -56,56 +56,57 @@ if (mongodUri.startsWith("file://")) {
56 56 mongodUri += "openWF";
57 57 }
58 58
59 mongoose
60 .connect(mongodUri)
61 .then(() => {
62 logger.info("Connected to MongoDB");
63 syncConfigWithDatabase();
59 try {
60 await mongoose.connect(mongodUri);
61 } catch (error) {
62 if (error instanceof Error) {
63 logger.error(`Error connecting to MongoDB server: ${error.message}`);
64 }
65 process.exit(1);
66 }
67 logger.info("Connected to MongoDB");
68 syncConfigWithDatabase();
69
70 try {
71 await startWebServer();
72 } catch (_err) {
73 const err = _err as IListenError;
74 if (err.port) {
75 logger.error(`Failed to bind port ${err.port}`);
76 if (process.platform == "win32") {
77 logger.error(
78 `You can check who has that port via powershell: Get-Process -Id (Get-NetTCPConnection -LocalPort ${err.port}).OwningProcess`
79 );
80 }
81 } else {
82 logger.error(err.message);
83 }
84 process.exit(1);
85 }
64 86
65 startWebServer().catch((err: IListenError) => {
66 if (err.port) {
67 logger.error(`Failed to bind port ${err.port}`);
68 if (process.platform == "win32") {
69 logger.error(
70 `You can check who has that port via powershell: Get-Process -Id (Get-NetTCPConnection -LocalPort ${err.port}).OwningProcess`
71 );
72 }
87 for (const [what, key] of [
88 ["IRC", "ircExecutable"],
89 ["HUB", "hubExecutable"]
90 ] as const) {
91 if (config[key]) {
92 logger.info(`Starting ${what}: ${config[key]}`);
93 child_process.execFile(config[key], (error, _stdout, _stderr) => {
94 if (error) {
95 logger.warn(`Failed to start ${what} server`, error);
73 96 } else {
74 logger.error(err.message);
97 logger.warn(`${what} server terminated unexpectedly`);
75 98 }
76 process.exit(1);
77 99 });
100 }
101 }
78 102
79 for (const [what, key] of [
80 ["IRC", "ircExecutable"],
81 ["HUB", "hubExecutable"]
82 ] as const) {
83 if (config[key]) {
84 logger.info(`Starting ${what}: ${config[key]}`);
85 child_process.execFile(config[key], (error, _stdout, _stderr) => {
86 if (error) {
87 logger.warn(`Failed to start ${what} server`, error);
88 } else {
89 logger.warn(`${what} server terminated unexpectedly`);
90 }
91 });
92 }
93 }
94
95 if (args.dev) {
96 logger.debug(
97 "Developer mode is enabled. Note that this project is where it is now due to code contributions; please pay it forward with pull requests."
98 );
99 }
103 if (args.dev) {
104 logger.debug(
105 "Developer mode is enabled. Note that this project is where it is now due to code contributions; please pay it forward with pull requests."
106 );
107 }
100 108
101 void updateWorldStateCollections();
102 setInterval(() => {
103 void updateWorldStateCollections();
104 }, 60_000);
105 })
106 .catch(error => {
107 if (error instanceof Error) {
108 logger.error(`Error connecting to MongoDB server: ${error.message}`);
109 }
110 process.exit(1);
111 });
109 void updateWorldStateCollections();
110 setInterval(() => {
111 void updateWorldStateCollections();
112 }, 60_000);