Skip to content

Commit

Permalink
SP-2273: Added some really convoluted error-checking code to try to r…
Browse files Browse the repository at this point in the history
…ecover from errors when callingDirectory.GetDirectories.
  • Loading branch information
tombogle committed Sep 7, 2022
1 parent 78d5415 commit 67a8d4e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 19 deletions.
1 change: 1 addition & 0 deletions SayMore.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=autofac/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=bldr/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Dropbox/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Jira/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Mets/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Palaso/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Parsable/@EntryIndexedValue">True</s:Boolean>
Expand Down
79 changes: 60 additions & 19 deletions src/SayMore/Model/ElementRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
using System.Xml;
using SIL.Code;
using SIL.Windows.Forms.FileSystem;
using SayMore.Model.Files;
using SayMore.UI.ProjectWindow;
using SIL.Reporting;
using L10NSharp;
using FileType = SayMore.Model.Files.FileType;
using System.Threading;

namespace SayMore.Model
{
/// ----------------------------------------------------------------------------------------
public class ElementIdChangedArgs : EventArgs
{
public ProjectElement Element { get; private set; }
public string OldId { get; private set; }
public string NewId { get; private set; }
public ProjectElement Element { get; }
public string OldId { get; }
public string NewId { get; }

/// ------------------------------------------------------------------------------------
public ElementIdChangedArgs(ProjectElement element, string oldId, string newId)
Expand All @@ -28,7 +30,7 @@ public ElementIdChangedArgs(ProjectElement element, string oldId, string newId)

/// ----------------------------------------------------------------------------------------
/// <summary>
/// This is reposible for finding, creating, and removing items of the given type T
/// This is responsible for finding, creating, and removing items of the given type T
/// (i.e. Sessions or People)
/// </summary>
/// ----------------------------------------------------------------------------------------
Expand Down Expand Up @@ -67,26 +69,71 @@ public ElementRepository(string projectDirectory, string elementGroupName,
[Obsolete("For Mocking Only")]
public ElementRepository(){}

public List<XmlException> FileLoadErrors { get; private set; }
public List<XmlException> FileLoadErrors { get; }

/// ------------------------------------------------------------------------------------
/// <summary>
/// Updates the list of items by looking in the file system for all the subfolders
/// in the project's folder corresponding to this repository.
/// </summary>
/// <remarks>Any file load errors ocurring during this call will be noted in
/// <remarks>Any file load errors occurring during this call will be noted in
/// FileLoadErrors. Caller is responsible for checking this and handling them as
/// appropriate.</remarks>
/// ------------------------------------------------------------------------------------
public void RefreshItemList()
{
FileLoadErrors.Clear();

var folders = new HashSet<string>(Directory.GetDirectories(_rootFolder));
// SP-2273...
string[] sessionDirectories = null;
var retry = false;
do
{
try
{
sessionDirectories = Directory.GetDirectories(_rootFolder);
}
catch (Exception e)
{
if (retry)
throw;

if (e is DirectoryNotFoundException)
{
Thread.Sleep(200); // Give it a fighting chance in case it was something transient.
if (Directory.Exists(_rootFolder))
{
retry = true;
continue;
}
}
if (e is IOException || e is UnauthorizedAccessException)
{
ErrorReport.ReportNonFatalExceptionWithMessage(e,
string.Format(LocalizationManager.GetString(
"MainWindow.ElementFolderMissingOrUnavailable",
"It looks like the {0} folder is no longer accessible. If you " +
"are able to fix this, {1} will retry. Otherwise, please report " +
"this ({2}).",
"Param 0: element type (\"Session\" or \"Person\"); "+
"Param 1: \"SayMore\" (program name); "+
"Param 2: Jira issue number (for developers)"),
ElementFileType.Name, Program.ProductName, "SP-2273"));
if (!Directory.Exists(_rootFolder))
throw;
retry = true;
}
else
throw;
}
} while (retry);
// ...SP-2273

var folders = new HashSet<string>(sessionDirectories);

// Go through the existing sessions we have and remove
// any that no longer have a sessions folder.
for (int i = _items.Count() - 1; i >= 0; i--)
for (int i = _items.Count - 1; i >= 0; i--)
{
if (!folders.Contains(_items[i].FolderPath))
_items.RemoveAt(i);
Expand Down Expand Up @@ -116,19 +163,13 @@ public void RefreshItemList()
}

/// ------------------------------------------------------------------------------------
public virtual IEnumerable<T> AllItems
{
get { return _items; }
}
public virtual IEnumerable<T> AllItems => _items;

/// ------------------------------------------------------------------------------------
public virtual string PathToFolder
{
get { return _rootFolder; }
}
public virtual string PathToFolder => _rootFolder;

/// ------------------------------------------------------------------------------------
public FileType ElementFileType { get; private set; }
public FileType ElementFileType { get; }

/// ------------------------------------------------------------------------------------
public T CreateNew(string id)
Expand Down

0 comments on commit 67a8d4e

Please sign in to comment.