Error Handling and Best Practices
Learn how to build robust, production-ready applications with proper error handling, authentication management, and performance optimization techniques.
Authentication Error Handling
try {
// Attempt authenticated operation
await manager.syncWithSyncServer();
} catch (error) {
if (error.message.includes("Unauthorized") || error.message.includes("401")) {
console.log("Authentication expired, re-authenticating...");
try {
await manager.login({
username: "admin@example.com",
password: "password123"
});
// Retry the operation
await manager.syncWithSyncServer();
} catch (authError) {
console.error("Re-authentication failed:", authError.message);
// Handle authentication failure
}
} else {
console.error("Sync error:", error.message);
}
}
Form Submission Error Handling
try {
// Submit form with invalid data
const invalidForm: FormSubmission = {
guid: uuidv4(),
type: "unknown-type", // This will cause an error
entityGuid: uuidv4(),
data: { name: "Test" },
timestamp: new Date().toISOString(),
userId: "user-id",
syncLevel: SyncLevel.LOCAL,
};
await manager.submitForm(invalidForm);
} catch (error) {
console.error("Form submission error:", error.message);
// Handle the error appropriately
}
Authentication State Management
// Check authentication before operations
const ensureAuthenticated = async () => {
if (!await manager.isAuthenticated()) {
throw new Error("Authentication required");
}
};
// Wrapper for authenticated operations
const authenticatedOperation = async (operation: () => Promise<any>) => {
await ensureAuthenticated();
try {
return await operation();
} catch (error) {
if (error.message.includes("Unauthorized")) {
// Re-authenticate and retry
await manager.login({
username: "admin@example.com",
password: "password123"
});
return await operation();
}
throw error;
}
};
// Usage
await authenticatedOperation(() => manager.syncWithSyncServer());
Cleanup and Resource Management
// Clean up stores and authentication when done
const cleanup = async () => {
await manager.logout();
entityStore.clearStore();
eventStore.clearStore();
authStorageAdapter.clearStore();
};
// Use in your application lifecycle
window.addEventListener('beforeunload', cleanup);
Performance Optimization
// Batch operations when possible
const batchCreateIndividuals = async (individuals: any[]) => {
// Ensure authentication once
await ensureAuthenticated();
const promises = individuals.map(individual =>
manager.submitForm({
guid: uuidv4(),
entityGuid: uuidv4(),
type: "create-individual",
data: individual,
timestamp: new Date().toISOString(),
userId: "user-id",
syncLevel: SyncLevel.LOCAL,
})
);
return await Promise.all(promises);
};
// Use for bulk operations
const manyIndividuals = [
{ name: "Person 1", age: 25 },
{ name: "Person 2", age: 30 },
{ name: "Person 3", age: 35 },
];
const createdIndividuals = await batchCreateIndividuals(manyIndividuals);
console.log("Batch created:", createdIndividuals.length);