Clean up exception and null handling in Import process

This commit is contained in:
Dean Herbert
2019-06-10 17:12:37 +09:00
parent 559413f766
commit c8bd92659b

View File

@ -252,15 +252,15 @@ namespace osu.Game.Database
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
TModel model;
try try
{ {
var model = CreateModel(archive); model = CreateModel(archive);
if (model == null) return null; if (model == null) return null;
model.Hash = computeHash(archive); model.Hash = computeHash(archive);
return await Import(model, archive, cancellationToken);
} }
catch (TaskCanceledException) catch (TaskCanceledException)
{ {
@ -271,6 +271,8 @@ namespace osu.Game.Database
Logger.Error(e, $"Model creation of {archive.Name} failed.", LoggingTarget.Database); Logger.Error(e, $"Model creation of {archive.Name} failed.", LoggingTarget.Database);
return null; return null;
} }
return await Import(model, archive, cancellationToken);
} }
/// <summary> /// <summary>
@ -340,7 +342,9 @@ namespace osu.Game.Database
Logger.Log($"Found existing {nameof(TModel)} for {item} (ID {existing.ID}). Skipping import.", LoggingTarget.Database); Logger.Log($"Found existing {nameof(TModel)} for {item} (ID {existing.ID}). Skipping import.", LoggingTarget.Database);
handleEvent(() => ItemAdded?.Invoke(existing, true)); handleEvent(() => ItemAdded?.Invoke(existing, true));
// existing item will be used; rollback new import and exit early.
rollback(); rollback();
flushEvents(true);
return existing; return existing;
} }
else else
@ -370,14 +374,11 @@ namespace osu.Game.Database
Logger.Error(e, $"Import of {item} failed and has been rolled back.", LoggingTarget.Database); Logger.Error(e, $"Import of {item} failed and has been rolled back.", LoggingTarget.Database);
rollback(); rollback();
item = null; flushEvents(false);
} throw;
finally
{
// we only want to flush events after we've confirmed the write context didn't have any errors.
flushEvents(item != null);
} }
flushEvents(true);
return item; return item;
}, cancellationToken, TaskCreationOptions.HideScheduler, IMPORT_SCHEDULER).Unwrap(); }, cancellationToken, TaskCreationOptions.HideScheduler, IMPORT_SCHEDULER).Unwrap();