Add error handling to ProductsGrid.DataGridView_CellContentClick

This commit is contained in:
Michael Bucari-Tovo 2022-06-23 22:32:43 -06:00
parent 76b5e09f72
commit b65875386d
3 changed files with 51 additions and 49 deletions

View File

@ -165,7 +165,7 @@ namespace FileLiberator
LameConfig = GetLameOptions(config) LameConfig = GetLameOptions(config)
}; };
var chapters = getChapters(contentLic.ContentMetadata.ChapterInfo.Chapters).OrderBy(c => c.StartOffsetMs).ToList(); var chapters = flattenChapters(contentLic.ContentMetadata.ChapterInfo.Chapters).OrderBy(c => c.StartOffsetMs).ToList();
if (config.AllowLibationFixup || outputFormat == OutputFormat.Mp3) if (config.AllowLibationFixup || outputFormat == OutputFormat.Mp3)
{ {
@ -192,7 +192,7 @@ namespace FileLiberator
return dlOptions; return dlOptions;
} }
public static List<AudibleApi.Common.Chapter> getChapters(IEnumerable<AudibleApi.Common.Chapter> chapters) public static List<AudibleApi.Common.Chapter> flattenChapters(IEnumerable<AudibleApi.Common.Chapter> chapters, string titleConcat = ": ")
{ {
List<AudibleApi.Common.Chapter> chaps = new(); List<AudibleApi.Common.Chapter> chaps = new();
@ -200,19 +200,14 @@ namespace FileLiberator
{ {
if (c.Chapters is not null) if (c.Chapters is not null)
{ {
c.Chapters[0] = new AudibleApi.Common.Chapter c.Chapters[0].StartOffsetMs = c.StartOffsetMs;
{ c.Chapters[0].StartOffsetSec = c.StartOffsetSec;
Title = c.Chapters[0].Title, c.Chapters[0].LengthMs += c.LengthMs;
StartOffsetMs = c.StartOffsetMs,
StartOffsetSec = c.StartOffsetSec,
LengthMs = c.LengthMs + c.Chapters[0].LengthMs,
Chapters = c.Chapters[0].Chapters
};
var children = getChapters(c.Chapters); var children = flattenChapters(c.Chapters);
foreach (var child in children) foreach (var child in children)
child.Title = string.IsNullOrEmpty(c.Title) ? child.Title : $"{c.Title}: {child.Title}"; child.Title = $"{c.Title}{titleConcat}{child.Title}";
chaps.AddRange(children); chaps.AddRange(children);
} }

View File

@ -49,6 +49,8 @@ namespace LibationWinForms.GridView
#region Button controls #region Button controls
private void DataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) private void DataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
try
{ {
// handle grid button click: https://stackoverflow.com/a/13687844 // handle grid button click: https://stackoverflow.com/a/13687844
if (e.RowIndex < 0) if (e.RowIndex < 0)
@ -91,6 +93,11 @@ namespace LibationWinForms.GridView
RemovableCountChanged?.Invoke(this, EventArgs.Empty); RemovableCountChanged?.Invoke(this, EventArgs.Empty);
} }
} }
catch(Exception ex)
{
Serilog.Log.Logger.Error(ex, $"An error was encountered while processing a user click in the {nameof(ProductsGrid)}");
}
}
private GridEntry getGridEntry(int rowIndex) => gridEntryDataGridView.GetBoundItem<GridEntry>(rowIndex); private GridEntry getGridEntry(int rowIndex) => gridEntryDataGridView.GetBoundItem<GridEntry>(rowIndex);

View File

@ -185,11 +185,11 @@ namespace FileLiberator.Tests
} }
}; };
var flatChapters = DownloadDecryptBook.getChapters(hierarchicalChapters).OrderBy(c => c.StartOffsetMs).ToArray(); var flatChapters = DownloadDecryptBook.flattenChapters(hierarchicalChapters);
flatChapters.Length.Should().Be(flatChapters.Length); flatChapters.Count.Should().Be(expected.Length);
for (int i = 0; i < flatChapters.Length; i++) for (int i = 0; i < flatChapters.Count; i++)
{ {
flatChapters[i].Title.Should().Be(expected[i].Title); flatChapters[i].Title.Should().Be(expected[i].Title);
flatChapters[i].StartOffsetMs.Should().Be(expected[i].StartOffsetMs); flatChapters[i].StartOffsetMs.Should().Be(expected[i].StartOffsetMs);