4. Error Handling
Lesson 4: Error Handling
RPCs can throw errors. LiveError lets you send structured error codes to the client.
LiveError
A regular throw new Error() inside an RPC is hidden from clients (security). To send an error the client can see, use LiveError:
throw new LiveError('CODE', 'Human-readable message') The client receives an object with code and message properties.
Try it
The counter goes negative when you click "-1" too many times. Fix it:
- Import
LiveErrorfromsvelte-realtime/server - In
decrement, check if count is already 0. If so, throw aLiveErrorwith code'INVALID' - In the component, catch the error when calling
decrement()and display it
Try it in the preview - click "-1" when the counter is at 0 and you should see your error message.
WebSocket
0
No messages yet