Error handling
The Lark SDK uses a consistent error model. Every error thrown by the SDK is a LarkError instance with a machine-readable code and a human-readable message.
Error structure
Error codes
Handling write errors
All write operations (set, update, remove, push) return promises. Wrap them in try/catch:
Handling read errors
Read operations (once, get) can also fail:
Handling transaction errors
Transactions can fail for additional reasons beyond normal write errors:
Connection-level errors
For errors that aren’t tied to a specific operation (transport failures, authentication errors, protocol issues), use db.onError():
db.onError() returns an unsubscribe function, just like other event listeners. Clean it up when you no longer need it.
Best practices
Always handle permission_denied errors in your UI. They usually mean a user is trying to access data they shouldn’t, either a bug in your security rules or a user navigating somewhere unexpected.
- Wrap writes in try/catch. Even if you’re confident your security rules will allow the write, network issues or data validation can cause failures.
- Log errors with their codes. The
code field is stable and machine-readable. Use it for programmatic decisions. Use message for human-readable logging.
- Use
db.onError() as a safety net. It catches connection-level issues that individual operation error handling might miss.
- Don’t swallow errors silently. At minimum, log them. Silently dropped errors make debugging much harder.