Handle xml errors in Persistent.Load gracefully
This commit is contained in:
@@ -7,6 +7,7 @@ using System.Text;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
|
using NLog;
|
||||||
|
|
||||||
namespace Torch
|
namespace Torch
|
||||||
{
|
{
|
||||||
@@ -17,6 +18,7 @@ namespace Torch
|
|||||||
/// <typeparam name="T">Data class type</typeparam>
|
/// <typeparam name="T">Data class type</typeparam>
|
||||||
public sealed class Persistent<T> : IDisposable where T : new()
|
public sealed class Persistent<T> : IDisposable where T : new()
|
||||||
{
|
{
|
||||||
|
private static Logger _log = LogManager.GetCurrentClassLogger();
|
||||||
public string Path { get; set; }
|
public string Path { get; set; }
|
||||||
private T _data;
|
private T _data;
|
||||||
public T Data
|
public T Data
|
||||||
@@ -77,6 +79,8 @@ namespace Torch
|
|||||||
Persistent<T> config = null;
|
Persistent<T> config = null;
|
||||||
|
|
||||||
if (File.Exists(path))
|
if (File.Exists(path))
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var ser = new XmlSerializer(typeof(T));
|
var ser = new XmlSerializer(typeof(T));
|
||||||
using (var f = File.OpenText(path))
|
using (var f = File.OpenText(path))
|
||||||
@@ -84,6 +88,12 @@ namespace Torch
|
|||||||
config = new Persistent<T>(path, (T)ser.Deserialize(f));
|
config = new Persistent<T>(path, (T)ser.Deserialize(f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_log.Error(ex);
|
||||||
|
config = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (config == null)
|
if (config == null)
|
||||||
config = new Persistent<T>(path, new T());
|
config = new Persistent<T>(path, new T());
|
||||||
if (!File.Exists(path) && saveIfNew)
|
if (!File.Exists(path) && saveIfNew)
|
||||||
|
Reference in New Issue
Block a user