fix: add try/catch around websocket message event handler #2529
@ -1,16 +1,11 @@
 | 
			
		||||
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 => {
 | 
			
		||||
    if (err.message == "Invalid accountId-nonce pair") {
 | 
			
		||||
        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 {
 | 
			
		||||
        logger.error(`uncaught error while processing ${req.path} request: ${err.message}`);
 | 
			
		||||
        logError(err, `processing ${req.path} request`);
 | 
			
		||||
        res.status(500).end();
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@ -5,6 +5,7 @@ import { Account } from "@/src/models/loginModel";
 | 
			
		||||
import { createAccount, createNonce, getUsernameFromEmail, isCorrectPassword } from "@/src/services/loginService";
 | 
			
		||||
import { IDatabaseAccountJson } from "@/src/types/loginTypes";
 | 
			
		||||
import { HydratedDocument } from "mongoose";
 | 
			
		||||
import { logError } from "@/src/utils/logger";
 | 
			
		||||
 | 
			
		||||
let wsServer: 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
 | 
			
		||||
    ws.on("message", async msg => {
 | 
			
		||||
        try {
 | 
			
		||||
            const data = JSON.parse(String(msg)) as IWsMsgFromClient;
 | 
			
		||||
            if (data.auth) {
 | 
			
		||||
                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}`));
 | 
			
		||||
errorLog.on("rotate", filename => logger.info(`Rotated error 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