How can understand error occured in backend using socket client in react?

31 Views Asked by At

My question is about socket io using nestjs and react. Here I am using nestjs for backend implementation and react for frontend implementation.

In Backend- I use nestjs webscoket with validation pipe. I also create a simple SubscribeMessage like this

@UsePipes(new ValidationPipe())
@SubscribeMessage("createMessage")
handleCreate(
    @MessageBody() messageInput: MessageInput
): WsResponse<string> {
    console.log(messageInput)
    return { event: "createMessage", data: "Hello world" }
}

Here you can see I am using ValidationPipe with class-validator.

It works perfectly. If I give wrong input it not continue the function. I am not sure, but It should throw an error message when I give wrong input which doesn't meet validation rules.

In frontend-

const MessageData = {
    conversation: "Some Data"
}
socket.emit("createMessage", MessageData)

I sent MessageData with conversation. When I emit this data I found my return data here-

socket.on('createMessage', (event) => {
    console.log('WebSocket error: ', event);
});

When It success, I also get return data here.

Questions: Here my questions is, When I give wrong input that doesn't meet validation rules, then how can I understand that there is going something wrong with my input. How can I catch this ValidationPipe error from frontend. I have no idea about this. Can anyone help me with that, How can I handle or catch or get error message in frontend which will be sent from backend when validation doesn't meet the requirements?

Anybody can gives me any idea?

0

There are 0 best solutions below