Skip to content

Commit 076fac0

Browse files
committed
add error handling docs
1 parent e3bdd80 commit 076fac0

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

docs/sdk-and-tools/rest-api/ws-subscriptions.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,60 @@ main().catch(console.error);
364364

365365
---
366366

367+
## Error Handling
368+
369+
Unexpected behaviors, such as sending an invalid payload or exceeding the server's subscription limits, will trigger an `error` event emitted by the server.
370+
371+
You should listen to this event to handle failures gracefully.
372+
373+
### Payload (DTO)
374+
375+
The error object contains context about which subscription failed and why.
376+
377+
| Field | Type | Description |
378+
|---------|--------|------------------------------------------------------------------------------------|
379+
| pattern | string | The subscription topic (event name) that was requested (e.g., `subscribeTransactions`). |
380+
| data | object | The original payload sent by the client that caused the error. |
381+
| error | object | The specific error returned by the server. |
382+
383+
### Example usage
384+
385+
```js
386+
import { io } from "socket.io-client";
387+
388+
// ... setup socket connection ...
389+
390+
// Listen for generic errors from the server
391+
socket.on("error", (errorData) => {
392+
console.error("Received error from server:");
393+
console.dir(errorData, { depth: null });
394+
});
395+
```
396+
397+
### Error Example
398+
399+
**Scenario:** The client attempts to open more subscriptions than the server allows (e.g., limit of X).
400+
401+
```json
402+
{
403+
"pattern": "subscribeCustomTransactions",
404+
"data": {
405+
"function": "newGame"
406+
},
407+
"error": "Maximum number of X global subscriptions accepted by server reached!"
408+
}
409+
```
410+
411+
---
412+
367413
## Summary
368414

369415
- WebSocket endpoint is dynamically obtained via `/websocket/config`.
370416
- Each stream has its own subscribe and update events.
371417
- Payload DTOs define allowed fields and required/optional rules.
372418
- Update messages mirror REST API and include `<resource>Count` fields.
373419
- `<resource>Count` reflects **total items at the moment of update**.
420+
- Errors are emitted via the standard `error` event, containing the pattern, original data, and error message.
374421
- Uses `socket.io-client`.
375422

376-
This document contains everything required to use MultiversX WebSocket Subscriptions effectively.
423+
This document contains everything required to use MultiversX WebSocket Subscriptions effectively.

0 commit comments

Comments
 (0)