diff --git a/Data/Serialization/FileSerializer.cs b/Data/Serialization/FileSerializer.cs index 70bf29d7..2da467a3 100644 --- a/Data/Serialization/FileSerializer.cs +++ b/Data/Serialization/FileSerializer.cs @@ -3,7 +3,6 @@ using System.IO; using System.Linq; using System.Reflection; -using System.Runtime.Serialization; using System.Text; using TweetDuck.Core.Utils; @@ -65,6 +64,8 @@ public void RegisterTypeConverter(Type type, ITypeConverter converter){ } public void Write(string file, T obj){ + LinkedList<string> errors = new LinkedList<string>(); + WindowsUtils.CreateDirectoryForFile(file); using(StreamWriter writer = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None))){ @@ -78,15 +79,21 @@ public void Write(string file, T obj){ if (serializer.TryWriteType(type, value, out string converted)){ if (converted != null){ - writer.Write($"{prop.Key} {EscapeLine(converted)}"); + writer.Write(prop.Key); + writer.Write(' '); + writer.Write(EscapeLine(converted)); writer.Write(NewLineReal); } } else{ - throw new SerializationException($"Invalid serialization type, conversion failed for: {type}"); + errors.AddLast($"Missing converter for type: {type}"); } } } + + if (errors.First != null){ + throw new SerializationSoftException(errors.ToArray()); + } } public void Read(string file, T obj){ @@ -103,6 +110,7 @@ public void Read(string file, T obj){ throw new FormatException("Input appears to be a binary file."); } + LinkedList<string> errors = new LinkedList<string>(); int currentPos = 0; do{ @@ -125,7 +133,8 @@ public void Read(string file, T obj){ int space = line.IndexOf(' '); if (space == -1){ - throw new SerializationException($"Invalid file format, missing separator: {line}"); + errors.AddLast($"Missing separator on line: {line}"); + continue; } string property = line.Substring(0, space); @@ -140,10 +149,14 @@ public void Read(string file, T obj){ info.SetValue(obj, converted); } else{ - throw new SerializationException($"Invalid file format, cannot convert value: {value} (property: {property})"); + errors.AddLast($"Failed reading property {property} with value: {value}"); } } }while(currentPos != -1); + + if (errors.First != null){ + throw new SerializationSoftException(errors.ToArray()); + } } public void ReadIfExists(string file, T obj){ diff --git a/Data/Serialization/SerializationSoftException.cs b/Data/Serialization/SerializationSoftException.cs new file mode 100644 index 00000000..2791fde4 --- /dev/null +++ b/Data/Serialization/SerializationSoftException.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; + +namespace TweetDuck.Data.Serialization{ + sealed class SerializationSoftException : Exception{ + public IList<string> Errors { get; } + + public SerializationSoftException(IList<string> errors) : base(string.Join(Environment.NewLine, errors)){ + this.Errors = errors; + } + } +} diff --git a/TweetDuck.csproj b/TweetDuck.csproj index 846eba1d..73b2e86e 100644 --- a/TweetDuck.csproj +++ b/TweetDuck.csproj @@ -247,6 +247,7 @@ <Compile Include="Data\Serialization\FileSerializer.cs" /> <Compile Include="Data\InjectedHTML.cs" /> <Compile Include="Data\Serialization\ITypeConverter.cs" /> + <Compile Include="Data\Serialization\SerializationSoftException.cs" /> <Compile Include="Data\Serialization\SingleTypeConverter.cs" /> <Compile Include="Data\TwoKeyDictionary.cs" /> <Compile Include="Data\WindowState.cs" />