30 lines
825 B
TypeScript
30 lines
825 B
TypeScript
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);
|
|
}
|
|
}
|