🐛 Fix(custom): fix error handling in server startup

This commit is contained in:
Kuingsmile
2024-03-21 13:04:31 +08:00
parent 475b85eb1f
commit cf0450f298
+2 -1
View File
@@ -164,10 +164,11 @@ class Server {
port = parseInt(port, 10) port = parseInt(port, 10)
} }
this.httpServer.listen(port, this.config.host).on('error', async (err: ErrnoException) => { this.httpServer.listen(port, this.config.host).on('error', async (err: ErrnoException) => {
if (err.errno === 'EADDRINUSE') { if (err.code === 'EADDRINUSE') {
try { try {
// make sure the system has a PicGo Server instance // make sure the system has a PicGo Server instance
await axios.post(ensureHTTPLink(`${this.config.host}:${port}/heartbeat`)) await axios.post(ensureHTTPLink(`${this.config.host}:${port}/heartbeat`))
logger.info(`[PicList Server] server is already running at ${port}`)
this.shutdown(true) this.shutdown(true)
} catch (e) { } catch (e) {
logger.warn(`[PicList Server] ${port} is busy, trying with port ${(port as number) + 1}`) logger.warn(`[PicList Server] ${port} is busy, trying with port ${(port as number) + 1}`)