Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get nestjs websocket exception error in client side using react?

I am try to create a realtime project for practice. I use nestjs for backend and react for frontend. Here in nestjs I am suing websocket.

In nestjs I write this code

@SubscribeMessage("createMessage")
handleCreate(
    @MessageBody() messageInput: MessageInput
): WsResponse<string> {
    console.log(messageInput)
    if (messageInput.conversation !== 12) throw new WsException("Error occured from siam!");
    return { event: "createMessage", data: "Hello world" }
}

Here I throw an WsException when conversation not equal to 12.

In frontend (reactjs) I emiting-

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

Here I sent 16 as conversation. It should throw an error from backend.

How can I get that error from frontend. Actually my question is where I found that WsException error in react application. I have not find any idea about. can anybody help me about this case, please.

like image 717
Jannat Avatar asked Mar 19 '26 23:03

Jannat


1 Answers

You should listen on a specific event: exception.

like image 71
MPCL5 Avatar answered Mar 22 '26 16:03

MPCL5



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!