Improved logging for import errors
This commit is contained in:
parent
c8c0ffeb0d
commit
5caa9c5687
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<Version>6.7.7.1</Version>
|
<Version>6.7.8.1</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@ -164,25 +164,46 @@ namespace ApplicationServices
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<int> importIntoDbAsync(List<ImportItem> importItems)
|
private static async Task<int> importIntoDbAsync(List<ImportItem> importItems)
|
||||||
|
{
|
||||||
|
logTime("importIntoDbAsync -- pre db");
|
||||||
|
using var context = DbContexts.GetContext();
|
||||||
|
var libraryBookImporter = new LibraryBookImporter(context);
|
||||||
|
var newCount = await Task.Run(() => libraryBookImporter.Import(importItems));
|
||||||
|
logTime("importIntoDbAsync -- post Import()");
|
||||||
|
int qtyChanges = saveChanges(context);
|
||||||
|
logTime("importIntoDbAsync -- post SaveChanges");
|
||||||
|
|
||||||
|
if (qtyChanges > 0)
|
||||||
|
await Task.Run(() => finalizeLibrarySizeChange());
|
||||||
|
logTime("importIntoDbAsync -- post finalizeLibrarySizeChange");
|
||||||
|
|
||||||
|
return newCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int saveChanges(LibationContext context)
|
||||||
{
|
{
|
||||||
logTime("importIntoDbAsync -- pre db");
|
try
|
||||||
using var context = DbContexts.GetContext();
|
{
|
||||||
var libraryBookImporter = new LibraryBookImporter(context);
|
return context.SaveChanges();
|
||||||
var newCount = await Task.Run(() => libraryBookImporter.Import(importItems));
|
}
|
||||||
logTime("importIntoDbAsync -- post Import()");
|
catch (Microsoft.EntityFrameworkCore.DbUpdateException ex)
|
||||||
var qtyChanges = context.SaveChanges();
|
{
|
||||||
logTime("importIntoDbAsync -- post SaveChanges");
|
// DbUpdateException exceptions can wreck serilog. Condense it until we can find a better solution. I suspect the culpret is the "WithExceptionDetails" serilog extension
|
||||||
|
|
||||||
if (qtyChanges > 0)
|
static string format(Exception ex) => $"\r\nMessage: {ex.Message}\r\nStack Trace:\r\n{ex.StackTrace}";
|
||||||
await Task.Run(() => finalizeLibrarySizeChange());
|
|
||||||
logTime("importIntoDbAsync -- post finalizeLibrarySizeChange");
|
|
||||||
|
|
||||||
return newCount;
|
var msg = "Microsoft.EntityFrameworkCore.DbUpdateException";
|
||||||
}
|
if (ex.InnerException is null)
|
||||||
#endregion
|
throw new Exception($"{msg}{format(ex)}");
|
||||||
|
throw new Exception(
|
||||||
|
$"{msg}{format(ex)}",
|
||||||
|
new Exception($"Inner Exception{format(ex.InnerException)}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region remove books
|
#region remove books
|
||||||
public static Task<List<LibraryBook>> RemoveBooksAsync(List<string> idsToRemove) => Task.Run(() => removeBooks(idsToRemove));
|
public static Task<List<LibraryBook>> RemoveBooksAsync(List<string> idsToRemove) => Task.Run(() => removeBooks(idsToRemove));
|
||||||
private static List<LibraryBook> removeBooks(List<string> idsToRemove)
|
private static List<LibraryBook> removeBooks(List<string> idsToRemove)
|
||||||
{
|
{
|
||||||
using var context = DbContexts.GetContext();
|
using var context = DbContexts.GetContext();
|
||||||
|
|||||||
@ -18,7 +18,7 @@ namespace DataLayer
|
|||||||
public class Category
|
public class Category
|
||||||
{
|
{
|
||||||
// Empty is a special case. use private ctor w/o validation
|
// Empty is a special case. use private ctor w/o validation
|
||||||
public static Category GetEmpty() => new Category { CategoryId = -1, AudibleCategoryId = "", Name = "" };
|
public static Category GetEmpty() => new() { CategoryId = -1, AudibleCategoryId = "", Name = "" };
|
||||||
|
|
||||||
internal int CategoryId { get; private set; }
|
internal int CategoryId { get; private set; }
|
||||||
public string AudibleCategoryId { get; private set; }
|
public string AudibleCategoryId { get; private set; }
|
||||||
|
|||||||
@ -7,7 +7,7 @@ namespace DataLayer
|
|||||||
public class Contributor
|
public class Contributor
|
||||||
{
|
{
|
||||||
// Empty is a special case. use private ctor w/o validation
|
// Empty is a special case. use private ctor w/o validation
|
||||||
public static Contributor GetEmpty() => new Contributor { ContributorId = -1, Name = "" };
|
public static Contributor GetEmpty() => new() { ContributorId = -1, Name = "" };
|
||||||
|
|
||||||
// contributors search links are just name with url-encoding. space can be + or %20
|
// contributors search links are just name with url-encoding. space can be + or %20
|
||||||
// author search link: /search?searchAuthor=Robert+Bevan
|
// author search link: /search?searchAuthor=Robert+Bevan
|
||||||
|
|||||||
@ -121,9 +121,7 @@ namespace DtoImporterService
|
|||||||
: item.Categories[1].CategoryId;
|
: item.Categories[1].CategoryId;
|
||||||
|
|
||||||
// This should properly be SingleOrDefault() not FirstOrDefault(), but FirstOrDefault is defensive
|
// This should properly be SingleOrDefault() not FirstOrDefault(), but FirstOrDefault is defensive
|
||||||
var category
|
var category = DbContext.Categories.Local.FirstOrDefault(c => c.AudibleCategoryId == lastCategory);
|
||||||
= DbContext.Categories.Local.FirstOrDefault(c => c.AudibleCategoryId == lastCategory)
|
|
||||||
?? Category.GetEmpty();
|
|
||||||
|
|
||||||
Book book;
|
Book book;
|
||||||
try
|
try
|
||||||
|
|||||||
@ -35,6 +35,9 @@ namespace DtoImporterService
|
|||||||
|
|
||||||
private void loadLocal_categories(List<string> categoryIds)
|
private void loadLocal_categories(List<string> categoryIds)
|
||||||
{
|
{
|
||||||
|
// must include default/empty/missing
|
||||||
|
categoryIds.Add(Category.GetEmpty().AudibleCategoryId);
|
||||||
|
|
||||||
var localIds = DbContext.Categories.Local.Select(c => c.AudibleCategoryId).ToList();
|
var localIds = DbContext.Categories.Local.Select(c => c.AudibleCategoryId).ToList();
|
||||||
var remainingCategoryIds = categoryIds
|
var remainingCategoryIds = categoryIds
|
||||||
.Distinct()
|
.Distinct()
|
||||||
@ -42,10 +45,8 @@ namespace DtoImporterService
|
|||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
// load existing => local
|
// load existing => local
|
||||||
// remember to include default/empty/missing
|
|
||||||
var emptyName = Contributor.GetEmpty().Name;
|
|
||||||
if (remainingCategoryIds.Any())
|
if (remainingCategoryIds.Any())
|
||||||
DbContext.Categories.Where(c => remainingCategoryIds.Contains(c.AudibleCategoryId) || c.Name == emptyName).ToList();
|
DbContext.Categories.Where(c => remainingCategoryIds.Contains(c.AudibleCategoryId)).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
// only use after loading contributors => local
|
// only use after loading contributors => local
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user