0/12
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:

  1. Import LiveError from svelte-realtime/server
  2. In decrement, check if count is already 0. If so, throw a LiveError with code 'INVALID'
  3. 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
User A
User B