Hi is this a good flow? If no, then how would you improve? How i do it right now is by using nested try-catch.
try {
// get the most recent created canister that will store the new data
switch (database.getLatestCanister()) {
case null throw Error.reject("No database canisters");
case (?db) await db.save(data);
}
} catch e1 {
// no canisters? or latest db canister has reached size limit?
// a new db canister is needed
Cycles.add(1_000_000_000_000);
let new_db = await Database.Canister();
// track the new db canister
let new_db_id = Principal.fromActor(new_canister);
database.add(new_db_id);
// retry saving the new data
try {
await new_db.save(data);
} catch e2 {
// total fail: return e1 and e2 errors
}
}