Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/25/09 16:45:09 (16 years ago)
Author:
epitzer
Message:

Collect all exceptions during serialization and continue as far as possible. Throw a collected exception in th end. (#678)

Location:
trunk/sources/HeuristicLab.Persistence
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/PersistenceException.cs

    r1823 r2106  
    44using HeuristicLab.Persistence.Core.Tokens;
    55using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     6using System.Text;
    67
    78namespace HeuristicLab.Persistence.Core {
     
    1213    public PersistenceException(string message) : base(message) { }
    1314    public PersistenceException(string message, Exception innerException) :  base(message, innerException) { }
     15    public PersistenceException(string message, IEnumerable<Exception> innerExceptions)
     16      : base(message) {
     17      int i = 0;
     18      foreach (var x in innerExceptions) {
     19        i += 1;
     20        this.Data.Add("Inner Exception " + i, x);
     21      }
     22    }
     23    public override string ToString() {
     24      var sb = new StringBuilder()
     25        .Append(base.ToString())
     26        .Append('\n');
     27      foreach (Exception x in Data.Values) {
     28        sb.Append(x.ToString()).Append('\n');
     29      }
     30      return sb.ToString();
     31    }
    1432  }
    1533 
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs

    r1823 r2106  
    3434    private readonly Dictionary<Type, int> typeCache;
    3535    private readonly Configuration configuration;
     36    private readonly bool isTestRun;
     37    private readonly List<Exception> exceptions;
    3638
    3739    public List<TypeMapping> TypeCache {
     
    7880      this(obj, configuration, "ROOT") { }
    7981
    80     public Serializer(object obj, Configuration configuration, string rootName) {
     82    public Serializer(object obj, Configuration configuration, string rootName)
     83      : this(obj, configuration, rootName, false) { }
     84
     85    public Serializer(object obj, Configuration configuration, string rootName, bool isTestRun) {
     86   
    8187      this.obj = obj;
    8288      this.rootName = rootName;
     
    8490      obj2id = new Dictionary<object, int>(new ReferenceEqualityComparer()) { { new object(), 0 } };
    8591      typeCache = new Dictionary<Type, int>();
     92      this.isTestRun = isTestRun;
     93      this.exceptions = new List<Exception>();
    8694    }
    8795
     
    9199
    92100    public IEnumerator<ISerializationToken> GetEnumerator() {
    93       return Serialize(new DataMemberAccessor(rootName, null, () => obj, null));
     101      var enumerator = Serialize(new DataMemberAccessor(rootName, null, () => obj, null));
     102      if (isTestRun) {
     103        return AddExceptionCompiler(enumerator);
     104      } else {
     105        return enumerator;
     106      }
     107    }
     108
     109    public IEnumerator<ISerializationToken> AddExceptionCompiler(IEnumerator<ISerializationToken> enumerator) {
     110      while (enumerator.MoveNext())
     111        yield return enumerator.Current;
     112      if (exceptions.Count == 1)
     113        throw exceptions[0];
     114      if (exceptions.Count > 1)
     115        throw new PersistenceException("Multiple exceptions during serialization", exceptions);
    94116    }
    95117
     
    109131        obj2id.Add(value, (int)id);
    110132      }
    111       IPrimitiveSerializer primitiveSerializer = configuration.GetPrimitiveSerializer(type);
    112       if (primitiveSerializer != null)
    113         return PrimitiveEnumerator(accessor.Name, typeId, primitiveSerializer.Format(value), id);
    114       ICompositeSerializer compositeSerializer = configuration.GetCompositeSerializer(type);
    115       if (compositeSerializer != null)
    116         return CompositeEnumerator(accessor.Name, compositeSerializer.Decompose(value), id, typeId, compositeSerializer.CreateMetaInfo(value));
    117       throw new PersistenceException(
    118           String.Format(
    119           "No suitable method for serializing values of type \"{0}\" found\r\n" +
    120           "primitive serializers:\r\n{1}\r\n" +
    121           "composite serializers:\r\n{2}",
    122           value.GetType().VersionInvariantName(),
    123           string.Join("\r\n", configuration.PrimitiveSerializers.Select(f => f.GetType().VersionInvariantName()).ToArray()),
    124           string.Join("\r\n", configuration.CompositeSerializers.Select(d => d.GetType().VersionInvariantName()).ToArray())
    125           ));
    126 
     133      try {
     134        IPrimitiveSerializer primitiveSerializer = configuration.GetPrimitiveSerializer(type);
     135        if (primitiveSerializer != null)
     136          return PrimitiveEnumerator(accessor.Name, typeId, primitiveSerializer.Format(value), id);
     137        ICompositeSerializer compositeSerializer = configuration.GetCompositeSerializer(type);
     138        if (compositeSerializer != null)
     139          return CompositeEnumerator(accessor.Name, compositeSerializer.Decompose(value), id, typeId, compositeSerializer.CreateMetaInfo(value));
     140        throw new PersistenceException(
     141            String.Format(
     142            "No suitable method for serializing values of type \"{0}\" found\r\n" +
     143            "primitive serializers:\r\n{1}\r\n" +
     144            "composite serializers:\r\n{2}",
     145            value.GetType().VersionInvariantName(),
     146            string.Join("\r\n", configuration.PrimitiveSerializers.Select(f => f.GetType().VersionInvariantName()).ToArray()),
     147            string.Join("\r\n", configuration.CompositeSerializers.Select(d => d.GetType().VersionInvariantName()).ToArray())
     148            ));
     149      } catch (Exception x) {
     150        if (isTestRun) {
     151          exceptions.Add(x);
     152          return new List<ISerializationToken>().GetEnumerator();
     153        } else {
     154          throw x;
     155        }
     156      }
    127157    }
    128158
  • trunk/sources/HeuristicLab.Persistence/UnitTests/UseCases.cs

    r1938 r2106  
    506506
    507507    [TestMethod]
     508    public void TestMultipleFailure() {
     509      List<NonSerializable> l = new List<NonSerializable>();
     510      l.Add(new NonSerializable());
     511      l.Add(new NonSerializable());
     512      l.Add(new NonSerializable());
     513      try {
     514        Serializer s = new Serializer(l,
     515          ConfigurationService.Instance.GetConfiguration(new XmlFormat()),
     516          "ROOT", true);
     517        StringBuilder tokens = new StringBuilder();
     518        foreach (var token in s) {
     519          tokens.Append(token.ToString());
     520        }
     521        Assert.Fail("Exception expected");
     522      } catch (PersistenceException px) {
     523        Assert.AreEqual(3, px.Data.Count);
     524      }
     525    }
     526
     527    [TestMethod]
    508528    public void TestAssemblyVersionCheck() {
    509529      IntWrapper i = new IntWrapper(1);
Note: See TracChangeset for help on using the changeset viewer.