Do not log < 500 on web sockets as errors

For example if someone spams a web socket without authentication we
should not log "forbidden".  Forbidden is normal/expected operation, not
an error.
This commit is contained in:
Asher 2023-05-01 10:41:10 -08:00
parent ff2764f7b1
commit aac5efa046
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
1 changed files with 5 additions and 1 deletions

View File

@ -62,12 +62,16 @@ export const errorHandler: express.ErrorRequestHandler = async (err, req, res, n
} }
export const wsErrorHandler: express.ErrorRequestHandler = async (err, req, res, next) => { export const wsErrorHandler: express.ErrorRequestHandler = async (err, req, res, next) => {
logger.error(`${err.message} ${err.stack}`)
let statusCode = 500 let statusCode = 500
if (errorHasStatusCode(err)) { if (errorHasStatusCode(err)) {
statusCode = err.statusCode statusCode = err.statusCode
} else if (errorHasCode(err) && notFoundCodes.includes(err.code)) { } else if (errorHasCode(err) && notFoundCodes.includes(err.code)) {
statusCode = HttpCode.NotFound statusCode = HttpCode.NotFound
} }
if (statusCode >= 500) {
logger.error(`${err.message} ${err.stack}`)
} else {
logger.debug(`${err.message} ${err.stack}`)
}
;(req as WebsocketRequest).ws.end(`HTTP/1.1 ${statusCode} ${err.message}\r\n\r\n`) ;(req as WebsocketRequest).ws.end(`HTTP/1.1 ${statusCode} ${err.message}\r\n\r\n`)
} }