fix: add try/catch around websocket message event handler
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				Build / build (pull_request) Failing after 1m15s
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	Build / build (pull_request) Failing after 1m15s
				
			This commit is contained in:
		
							parent
							
								
									0f6b55beed
								
							
						
					
					
						commit
						65a41ccc68
					
				@ -1,16 +1,11 @@
 | 
				
			|||||||
import { NextFunction, Request, Response } from "express";
 | 
					import { NextFunction, Request, Response } from "express";
 | 
				
			||||||
import { logger } from "@/src/utils/logger";
 | 
					import { logError } from "@/src/utils/logger";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const errorHandler = (err: Error, req: Request, res: Response, _next: NextFunction): void => {
 | 
					export const errorHandler = (err: Error, req: Request, res: Response, _next: NextFunction): void => {
 | 
				
			||||||
    if (err.message == "Invalid accountId-nonce pair") {
 | 
					    if (err.message == "Invalid accountId-nonce pair") {
 | 
				
			||||||
        res.status(400).send("Log-in expired");
 | 
					        res.status(400).send("Log-in expired");
 | 
				
			||||||
    } else if (err.stack) {
 | 
					 | 
				
			||||||
        const stackArr = err.stack.split("\n");
 | 
					 | 
				
			||||||
        stackArr[0] += ` while processing ${req.path} request`;
 | 
					 | 
				
			||||||
        logger.error(stackArr.join("\n"));
 | 
					 | 
				
			||||||
        res.status(500).end();
 | 
					 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        logger.error(`uncaught error while processing ${req.path} request: ${err.message}`);
 | 
					        logError(err, `processing ${req.path} request`);
 | 
				
			||||||
        res.status(500).end();
 | 
					        res.status(500).end();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -5,6 +5,7 @@ import { Account } from "@/src/models/loginModel";
 | 
				
			|||||||
import { createAccount, createNonce, getUsernameFromEmail, isCorrectPassword } from "@/src/services/loginService";
 | 
					import { createAccount, createNonce, getUsernameFromEmail, isCorrectPassword } from "@/src/services/loginService";
 | 
				
			||||||
import { IDatabaseAccountJson } from "@/src/types/loginTypes";
 | 
					import { IDatabaseAccountJson } from "@/src/types/loginTypes";
 | 
				
			||||||
import { HydratedDocument } from "mongoose";
 | 
					import { HydratedDocument } from "mongoose";
 | 
				
			||||||
 | 
					import { logError } from "../utils/logger";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let wsServer: ws.Server | undefined;
 | 
					let wsServer: ws.Server | undefined;
 | 
				
			||||||
let wssServer: ws.Server | undefined;
 | 
					let wssServer: ws.Server | undefined;
 | 
				
			||||||
@ -88,6 +89,7 @@ const wsOnConnect = (ws: ws, req: http.IncomingMessage): void => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // eslint-disable-next-line @typescript-eslint/no-misused-promises
 | 
					    // eslint-disable-next-line @typescript-eslint/no-misused-promises
 | 
				
			||||||
    ws.on("message", async msg => {
 | 
					    ws.on("message", async msg => {
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
            const data = JSON.parse(String(msg)) as IWsMsgFromClient;
 | 
					            const data = JSON.parse(String(msg)) as IWsMsgFromClient;
 | 
				
			||||||
            if (data.auth) {
 | 
					            if (data.auth) {
 | 
				
			||||||
                let account: IDatabaseAccountJson | null = await Account.findOne({ email: data.auth.email });
 | 
					                let account: IDatabaseAccountJson | null = await Account.findOne({ email: data.auth.email });
 | 
				
			||||||
@ -146,6 +148,9 @@ const wsOnConnect = (ws: ws, req: http.IncomingMessage): void => {
 | 
				
			|||||||
                    }
 | 
					                    }
 | 
				
			||||||
                );
 | 
					                );
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					        } catch (e) {
 | 
				
			||||||
 | 
					            logError(e as Error, `processing websocket message`);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -108,3 +108,13 @@ errorLog.on("new", filename => logger.info(`Using error log file: ${filename}`))
 | 
				
			|||||||
combinedLog.on("new", filename => logger.info(`Using combined log file: ${filename}`));
 | 
					combinedLog.on("new", filename => logger.info(`Using combined log file: ${filename}`));
 | 
				
			||||||
errorLog.on("rotate", filename => logger.info(`Rotated error log file: ${filename}`));
 | 
					errorLog.on("rotate", filename => logger.info(`Rotated error log file: ${filename}`));
 | 
				
			||||||
combinedLog.on("rotate", filename => logger.info(`Rotated combined log file: ${filename}`));
 | 
					combinedLog.on("rotate", filename => logger.info(`Rotated combined log file: ${filename}`));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const logError = (err: Error, context: string): void => {
 | 
				
			||||||
 | 
					    if (err.stack) {
 | 
				
			||||||
 | 
					        const stackArr = err.stack.split("\n");
 | 
				
			||||||
 | 
					        stackArr[0] += ` while ${context}`;
 | 
				
			||||||
 | 
					        logger.error(stackArr.join("\n"));
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        logger.error(`uncaught error while ${context}: ${err.message}`);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user