show friendly error when parsing a manifest version fails (#308)

This commit is contained in:
Jesse Plamondon-Willard 2017-06-18 22:11:48 -04:00
parent b46776a4fb
commit fb8fefea00
4 changed files with 33 additions and 2 deletions

View File

@ -0,0 +1,17 @@
using System;
namespace StardewModdingAPI.Framework.Exceptions
{
/// <summary>A format exception which provides a user-facing error message.</summary>
internal class SParseException : FormatException
{
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="message">The error message.</param>
/// <param name="ex">The underlying exception, if any.</param>
public SParseException(string message, Exception ex = null)
: base(message, ex) { }
}
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using StardewModdingAPI.Framework.Exceptions;
using StardewModdingAPI.Framework.Models;
using StardewModdingAPI.Framework.Serialisation;
@ -45,6 +46,10 @@ namespace StardewModdingAPI.Framework.ModLoading
else if (string.IsNullOrWhiteSpace(manifest.EntryDll))
error = "its manifest doesn't set an entry DLL.";
}
catch (SParseException ex)
{
error = $"parsing its manifest failed: {ex.Message}";
}
catch (Exception ex)
{
error = $"parsing its manifest failed:\n{ex.GetLogSummary()}";

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StardewModdingAPI.Framework.Exceptions;
using StardewModdingAPI.Framework.Models;
namespace StardewModdingAPI.Framework.Serialisation
@ -50,10 +51,17 @@ namespace StardewModdingAPI.Framework.Serialisation
}
case JTokenType.String:
return new SemanticVersion(token.Value<string>());
{
string str = token.Value<string>();
if (string.IsNullOrWhiteSpace(str))
return null;
if (!SemanticVersion.TryParse(str, out ISemanticVersion version))
throw new SParseException($"Can't parse semantic version from invalid value '{str}', should be formatted like 1.2, 1.2.30, or 1.2.30-beta.");
return version;
}
default:
throw new FormatException($"Can't parse {token.Type} token as a semantic version, must be an object or string.");
throw new SParseException($"Can't parse semantic version from {token.Type}, must be an object or string.");
}
}

View File

@ -151,6 +151,7 @@
<Compile Include="Framework\Reflection\PrivateProperty.cs" />
<Compile Include="Framework\RequestExitDelegate.cs" />
<Compile Include="Framework\SContentManager.cs" />
<Compile Include="Framework\Exceptions\SParseException.cs" />
<Compile Include="Framework\Serialisation\JsonHelper.cs" />
<Compile Include="Framework\Serialisation\SelectiveStringEnumConverter.cs" />
<Compile Include="Framework\Serialisation\ManifestFieldConverter.cs" />