Error Handling
Errors happen. PaindaProtocol captures server-side logic and validation errors and safely transmits them back to the client.
1. Acknowledgement Errors
When using Request/Response patterns (Acknowledgements), the first argument in the callback is reserved for the Error, following the standard Node.js convention (error-first callbacks).
// Client
client.send({ type: "authenticate", payload: "fake-jwt" }, (err, data) => {
if (err) {
// The server called ack(new Error("..."))
console.error("Auth Error:", err.message);
return;
}
console.log("Welcome!", data);
});
// Server
server.on("authenticate", (client, token, ack) => {
if (!isValid(token)) {
// Sending a standard Error object will be serialized automatically
ack(new Error("Invalid authorization token provided"));
} else {
ack(null, { status: "success" });
}
});2. Global Client Errors
Some errors don't belong to a specific callback. For example: malformed message frames, connection timeouts, or protocol version mismatches.
client.on("error", (err) => {
// Global catch for connection and parsing errors
console.error("PaindaProtocol exception:", err);
});3. Disconnect Reasons
When a connection drops, the disconnect event emits a reason string. This helps you distinguish between network issues and intentional kicks.
client.on("disconnect", (reason) => {
if (reason === "transport close") {
console.log("Server crashed or network dropped.");
} else if (reason === "Ping timeout") {
console.log("Client failed to respond to server pings.");
} else if (reason === "forced server disconnect") {
console.log("You were kicked by the server-side logic.");
}
});Uncaught Server Exceptions
If your server throws an unhandled synchronous exception inside an event handler, PaindaProtocol will catch it and prevent the Node.js process from crashing. It will automatically log the error under the hood. However, for async/await handlers, make sure to wrap your code in try/catch blocks!