feat: ws simple setup only
This commit is contained in:
@@ -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,
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
||||
29
common/exceptions/websocket.ts
Normal file
29
common/exceptions/websocket.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user