Skip to content

Commit fbc5f4d

Browse files
Handle critical import errors
1 parent 20a786a commit fbc5f4d

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

JournalApp/Data/AppDataService.cs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public async Task<bool> StartImportWizard(IDialogService dialogService, string p
1010

1111
// Warn if an export wasn't done in the last week.
1212
if (DateTimeOffset.Now > LastExportDate.AddDays(7) &&
13-
await dialogService.ShowCustomMessageBox(string.Empty, "It's recommended to export your data first. You can do this in Settings.", yesText: "Continue anyway", cancelText: "Go back") == null)
13+
await dialogService.ShowCustomMessageBox(string.Empty, "It's recommended to export your data first in case there are any issues; You can do this in Settings.", yesText: "Continue anyway", cancelText: "Go back") == null)
1414
{
1515
logger.LogDebug("User didn't want to import after being warned about export");
1616
return false;
@@ -56,25 +56,33 @@ public async Task<bool> StartImportWizard(IDialogService dialogService, string p
5656
logger.LogInformation($"Preference set: {key}");
5757
}
5858

59-
// Apply the backup content to the database.
60-
sw.Restart();
61-
await using (var db = await dbFactory.CreateDbContextAsync())
59+
try
6260
{
63-
db.Days.RemoveRange(db.Days);
64-
db.Categories.RemoveRange(db.Categories);
65-
db.Points.RemoveRange(db.Points);
66-
await db.SaveChangesAsync();
67-
logger.LogDebug($"Cleared old db sets after {sw.ElapsedMilliseconds}ms");
68-
}
61+
sw.Restart();
62+
await using (var db = await dbFactory.CreateDbContextAsync())
63+
{
64+
db.Days.RemoveRange(db.Days);
65+
db.Categories.RemoveRange(db.Categories);
66+
db.Points.RemoveRange(db.Points);
67+
await db.SaveChangesAsync();
68+
logger.LogDebug($"Cleared old db sets after {sw.ElapsedMilliseconds}ms");
69+
}
6970

70-
sw.Restart();
71-
await using (var db = await dbFactory.CreateDbContextAsync())
71+
sw.Restart();
72+
await using (var db = await dbFactory.CreateDbContextAsync())
73+
{
74+
await db.Days.AddRangeAsync(backup.Days);
75+
await db.Categories.AddRangeAsync(backup.Categories);
76+
await db.Points.AddRangeAsync(backup.Points);
77+
await db.SaveChangesAsync();
78+
logger.LogDebug($"Added new data after {sw.ElapsedMilliseconds}ms");
79+
}
80+
}
81+
catch (Exception ex)
7282
{
73-
await db.Days.AddRangeAsync(backup.Days);
74-
await db.Categories.AddRangeAsync(backup.Categories);
75-
await db.Points.AddRangeAsync(backup.Points);
76-
await db.SaveChangesAsync();
77-
logger.LogDebug($"Added new data after {sw.ElapsedMilliseconds}ms");
83+
logger.LogError(ex, $"Import failed during database changes after {sw.ElapsedMilliseconds}ms");
84+
await dialogService.ShowCustomMessageBox(string.Empty, $"Import critically failed; Database is potentially corrupt and app may need to be reinstalled due to error: {ex}.", showFeedbackLink: true);
85+
return false;
7886
}
7987

8088
LastExportDate = DateTimeOffset.Now;

0 commit comments

Comments
 (0)