Skip to content

Commit

Permalink
Propogate error messages out of deserialization
Browse files Browse the repository at this point in the history
This is a breaking API change.
  • Loading branch information
jacobdufault committed Feb 11, 2014
1 parent b095594 commit aad3948
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
11 changes: 11 additions & 0 deletions Forge.Entities/API/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
using System;

namespace Forge.Entities {
/// <summary>
/// Exception thrown when a deserialization error has occurred. This is most likely during a
/// level load.
/// </summary>
[Serializable]
public class DeserializationException : Exception {
internal DeserializationException(string json, Exception e) :
base("error " + e + " when deserializing " + json) {
}
}

/// <summary>
/// Exception thrown when a data type is added to an entity, but the entity already contains an
/// instance of said data type.
Expand Down
32 changes: 14 additions & 18 deletions Forge.Entities/API/LevelManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,8 @@ public static string MergeTemplateGroups(IEnumerable<string> groups) {
// deserialize all of the groups
List<ITemplateGroup> deserializedGroups = new List<ITemplateGroup>();
foreach (string serializedGroup in groups) {
Maybe<ITemplateGroup> group = LoadTemplateGroup(serializedGroup);
if (group.IsEmpty) {
throw new InvalidOperationException("Unable to convert " + serializedGroup +
" to an ITemplateGroup");
}

deserializedGroups.Add(group.Value);
var group = LoadTemplateGroup(serializedGroup);
deserializedGroups.Add(group);
}

// merge them
Expand All @@ -94,6 +89,7 @@ public static string MergeTemplateGroups(IEnumerable<string> groups) {
/// <param name="groups">The template groups to merge.</param>
/// <returns>A single template group that contains all of the templates within the given
/// groups.</returns>
/// <exception cref="InvalidOperationException">There were duplicate TemplateIds</exception>
public static ITemplateGroup MergeTemplateGroups(IEnumerable<ITemplateGroup> groups) {
// We store all of our templates in result.
var result = new TemplateGroup();
Expand Down Expand Up @@ -143,37 +139,37 @@ public static string SaveTemplateGroup(ITemplateGroup templates) {
/// Loads an IGameSnapshot from the given JSON and the given template group. The JSON should
/// have been generated by calling SaveSnapshot.
/// </summary>
public static Maybe<IGameSnapshot> LoadSnapshot(string snapshotJson, string templateJson) {
/// <exception cref="DeserializationException">An error loading the snapshot
/// occurred.</exception>
public static IGameSnapshot LoadSnapshot(string snapshotJson, string templateJson) {
try {
var restorer = GameSnapshotRestorer.Restore(snapshotJson, templateJson,
Maybe<GameEngine>.Empty);
return Maybe.Just<IGameSnapshot>(restorer.GameSnapshot);
return restorer.GameSnapshot;
}
catch (Exception e) {
Log.Get(typeof(LevelManager)).Warn(string.Format("Unable to load snapshot from " +
"snapshot={0}, template={1} with error={2}", snapshotJson, templateJson, e));
throw new DeserializationException("snapshot=" + snapshotJson +
", template=" + templateJson, e);
}
return Maybe<IGameSnapshot>.Empty;
}

/// <summary>
/// Loads an ITemplateGroup from the given JSON. The JSON should have been generated by
/// calling SaveTemplates.
/// </summary>
public static Maybe<ITemplateGroup> LoadTemplateGroup(string json) {
/// <exception cref="DeserializationException">An error loading the group
/// occurred.</exception>
public static ITemplateGroup LoadTemplateGroup(string json) {
try {
ITemplateGroup group = SerializationHelpers.Deserialize<TemplateGroup>(json,
RequiredConverters.GetConverters(),
RequiredConverters.GetContextObjects(Maybe<GameEngine>.Empty,
new TemplateConversionContext()));
return Maybe.Just(group);
return group;
}
catch (Exception e) {
Log.Get(typeof(LevelManager)).Warn(string.Format("Unable to load template group " +
"from templates={0} with error={1}", json, e));
throw new DeserializationException(json, e);
}

return Maybe<ITemplateGroup>.Empty;
}
}
}

0 comments on commit aad3948

Please sign in to comment.