feat: ws simple setup only

This commit is contained in:
SauravDhakal
2026-04-12 21:50:46 +05:45
parent 2f01adeade
commit 4686e0abbc
13 changed files with 334 additions and 23 deletions

View File

@@ -9,13 +9,14 @@ import { Request, Response } from 'express';
@Catch(HttpException) // What exception to catch
export class HttpExceptionFilter implements ExceptionFilter<HttpException> {
constructor(private readonly logger: Logger) {}
constructor(private readonly logger: Logger) { }
catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const request: Request = ctx.getRequest();
const response: Response = ctx.getResponse();
const status = exception.getStatus();
if (status >= 500) {
this.logger.warn({
method: request.method,
@@ -28,10 +29,11 @@ export class HttpExceptionFilter implements ExceptionFilter<HttpException> {
exception.message = `${exception.message} not found`;
}
response.status(status).json({
success: false,
message: exception.message,
statusCode: status,
});
// response.status(status).json({
// success: false,
// message: exception.message,
// statusCode: status,
// });
}
}

View File

@@ -0,0 +1,29 @@
import { Catch, ArgumentsHost } from '@nestjs/common';
import { BaseWsExceptionFilter, WsException } from '@nestjs/websockets';
@Catch()
export class WsValidationExceptionFilter extends BaseWsExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
const client = host.switchToWs().getClient();
if (exception instanceof WsException) {
client.emit('exception', {
status: 'error',
message: exception.getError(),
});
return;
}
// Handle ValidationPipe errors (they come as plain objects, not WsException)
if (
Array.isArray((exception as any)?.response?.message)
) {
client.emit('exception', {
status: 'error',
message: (exception as any).response.message,
});
return;
}
super.catch(exception, host);
}
}