Skip to content

Commit d6a37f5

Browse files
committed
Added try-catch so that errors during parse would not be ignored.
Added error-handler to Paratext-based constructor so Vessel can specify its own error handling.
1 parent 80f9fa1 commit d6a37f5

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

Diff for: GlyssenEngine/Project.cs

+38-17
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public Project(GlyssenBundle bundle, string recordingProjectName = null, Project
194194
PopulateAndParseBooks(bundle);
195195
}
196196

197-
public Project(ParatextScrTextWrapper paratextProject) :
197+
public Project(ParatextScrTextWrapper paratextProject, Action<Exception> exceptionHandler = null) :
198198
this(paratextProject.GlyssenDblTextMetadata, null, false, paratextProject.WritingSystem)
199199
{
200200
Writer.SetUpProjectPersistence(this);
@@ -204,7 +204,7 @@ public Project(ParatextScrTextWrapper paratextProject) :
204204
SetWsQuotationMarksUsingFullySpecifiedContinuers(paratextProject.QuotationMarks);
205205
}
206206

207-
ParseAndSetBooks(paratextProject.UsxDocumentsForIncludedBooks, paratextProject.Stylesheet);
207+
ParseAndSetBooks(paratextProject.UsxDocumentsForIncludedBooks, paratextProject.Stylesheet, exceptionHandler);
208208
}
209209

210210
/// <summary>
@@ -1440,7 +1440,7 @@ public void IncludeExistingBook(BookScript book)
14401440
m_books.Insert(i, book);
14411441
}
14421442

1443-
public void IncludeBooksFromParatext(ParatextScrTextWrapper wrapper, ISet<int> bookNumbers, Action<BookScript> postParseAction)
1443+
public async Task IncludeBooksFromParatext(ParatextScrTextWrapper wrapper, ISet<int> bookNumbers, Action<BookScript> postParseAction)
14441444
{
14451445
wrapper.IncludeBooks(bookNumbers.Select(BCVRef.NumberToBookCode));
14461446
var usxBookInfoList = wrapper.GetUsxDocumentsForIncludedParatextBooks(bookNumbers);
@@ -1453,25 +1453,49 @@ void EnhancedPostParseAction(BookScript book)
14531453
postParseAction?.Invoke(book);
14541454
}
14551455

1456-
ParseAndIncludeBooks(usxBookInfoList, wrapper.Stylesheet, EnhancedPostParseAction);
1456+
await ParseAndIncludeBooks(usxBookInfoList, wrapper.Stylesheet, null, EnhancedPostParseAction);
14571457
}
14581458

1459-
private void ParseAndSetBooks(IEnumerable<UsxDocument> books, IStylesheet stylesheet)
1459+
private async Task ParseAndSetBooks(IEnumerable<UsxDocument> books, IStylesheet stylesheet, Action<Exception> exceptionHandler = null)
14601460
{
14611461
if (m_books.Any())
14621462
throw new InvalidOperationException("Project already contains books. If the intention is to replace the existing ones, let's clear the list first. Otherwise, call ParseAndIncludeBooks.");
1463-
ParseAndIncludeBooks(books, stylesheet);
1463+
await ParseAndIncludeBooks(books, stylesheet, exceptionHandler);
14641464
}
14651465

1466-
private async Task ParseAndIncludeBooks(IEnumerable<UsxDocument> books, IStylesheet stylesheet, Action<BookScript> postParseAction = null)
1466+
private async Task ParseAndIncludeBooks(IEnumerable<UsxDocument> books, IStylesheet stylesheet,
1467+
Action<Exception> exceptionHandler/* = null*/, Action<BookScript> postParseAction = null)
14671468
{
14681469
if (Versification == null)
14691470
throw new NullReferenceException("What!!!");
14701471
ProjectState = ProjectState.Initial | (ProjectState & ProjectState.WritingSystemRecoveryInProcess);
1471-
await Task.Run(() => {UsxParse(books, stylesheet, postParseAction);});
1472+
List<BookScript> parsedBooks = null;
1473+
try
1474+
{
1475+
await Task.Run(() =>
1476+
{
1477+
parsedBooks = UsxParse(books, stylesheet, postParseAction);
1478+
});
1479+
1480+
await Task.Run(() =>
1481+
{
1482+
if (QuoteSystem == null)
1483+
GuessAtQuoteSystem();
1484+
else if (IsQuoteSystemReadyForParse)
1485+
DoQuoteParse(parsedBooks.Select(b => b.BookId));
1486+
});
1487+
}
1488+
catch (Exception e)
1489+
{
1490+
Console.WriteLine(e);
1491+
if (exceptionHandler == null)
1492+
ErrorReport.ReportFatalException(e);
1493+
else
1494+
exceptionHandler(e);
1495+
}
14721496
}
14731497

1474-
private void UsxParse(IEnumerable<UsxDocument> books, IStylesheet stylesheet, Action<BookScript> postParseAction = null)
1498+
private List<BookScript> UsxParse(IEnumerable<UsxDocument> books, IStylesheet stylesheet, Action<BookScript> postParseAction = null)
14751499
{
14761500
var parsedBooks = UsxParser.ParseBooks(books, stylesheet, i =>
14771501
{
@@ -1517,20 +1541,17 @@ private void UsxParse(IEnumerable<UsxDocument> books, IStylesheet stylesheet, Ac
15171541
AddMissingAvailableBooks();
15181542
}
15191543

1520-
if (QuoteSystem == null)
1521-
GuessAtQuoteSystemAsync();
1522-
else if (IsQuoteSystemReadyForParse)
1523-
DoQuoteParseAsync(parsedBooks.Select(b => b.BookId));
1544+
return parsedBooks;
15241545
}
15251546

15261547
private async Task GuessAtQuoteSystemAsync()
15271548
{
1528-
ProjectState = ProjectState.UsxComplete | (ProjectState & ProjectState.WritingSystemRecoveryInProcess);
15291549
await Task.Run(GuessAtQuoteSystem);
15301550
}
15311551

15321552
private void GuessAtQuoteSystem()
15331553
{
1554+
ProjectState = ProjectState.UsxComplete | (ProjectState & ProjectState.WritingSystemRecoveryInProcess);
15341555
var quoteSystem = QuoteSystemGuesser.Guess(ControlCharacterVerseData.Singleton, m_books, Versification, out _,
15351556
i =>
15361557
{
@@ -1539,19 +1560,19 @@ private void GuessAtQuoteSystem()
15391560
OnReport(pe);
15401561
});
15411562

1542-
SetQuoteSystem(QuoteSystemStatus.Guessed, quoteSystem);
1563+
SetQuoteSystem(QuoteSystemStatus.Guessed, quoteSystem);
15431564
Save();
15441565
}
15451566

15461567
private async Task DoQuoteParseAsync(IEnumerable<string> booksToParse = null)
15471568
{
1548-
m_projectMetadata.ParserVersion = kParserVersion;
1549-
ProjectState = ProjectState.Parsing;
15501569
await Task.Run(() => { DoQuoteParse(booksToParse);});
15511570
}
15521571

15531572
private void DoQuoteParse(IEnumerable<string> bookIds)
15541573
{
1574+
m_projectMetadata.ParserVersion = kParserVersion;
1575+
ProjectState = ProjectState.Parsing;
15551576
try
15561577
{
15571578
QuoteParser.ParseProject(this, i =>

0 commit comments

Comments
 (0)