improve mod update-check validation & errors (#336)

This commit is contained in:
Jesse Plamondon-Willard 2017-09-25 21:21:27 -04:00
parent 5cb183e16d
commit 2c87961c9e
4 changed files with 22 additions and 2 deletions

View File

@ -106,14 +106,14 @@ namespace StardewModdingAPI.Web.Controllers
// parse mod key // parse mod key
if (!this.TryParseModKey(modKey, out string vendorKey, out string modID)) if (!this.TryParseModKey(modKey, out string vendorKey, out string modID))
{ {
result[modKey] = new ModInfoModel("The mod key isn't in a valid format. It should contain the mod repository key and mod ID like 'Nexus:541'."); result[modKey] = new ModInfoModel("The mod key isn't in a valid format. It should contain the site key and mod ID like 'Nexus:541'.");
continue; continue;
} }
// get matching repository // get matching repository
if (!this.Repositories.TryGetValue(vendorKey, out IModRepository repository)) if (!this.Repositories.TryGetValue(vendorKey, out IModRepository repository))
{ {
result[modKey] = new ModInfoModel("There's no mod repository matching this namespaced mod ID."); result[modKey] = new ModInfoModel($"There's no mod site with key '{vendorKey}'. Expected one of [{string.Join(", ", this.Repositories.Keys)}].");
continue; continue;
} }

View File

@ -43,6 +43,11 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories
/// <param name="id">The mod ID in this repository.</param> /// <param name="id">The mod ID in this repository.</param>
public override async Task<ModInfoModel> GetModInfoAsync(string id) public override async Task<ModInfoModel> GetModInfoAsync(string id)
{ {
// validate ID format
if (!uint.TryParse(id, out uint _))
return new ModInfoModel($"The value '{id}' isn't a valid Chucklefish mod ID, must be an integer ID.");
// fetch info
try try
{ {
// fetch HTML // fetch HTML

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using Pathoschild.Http.Client; using Pathoschild.Http.Client;
@ -46,6 +47,11 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories
/// <param name="id">The mod ID in this repository.</param> /// <param name="id">The mod ID in this repository.</param>
public override async Task<ModInfoModel> GetModInfoAsync(string id) public override async Task<ModInfoModel> GetModInfoAsync(string id)
{ {
// validate ID format
if (!id.Contains("/") || id.IndexOf("/", StringComparison.InvariantCultureIgnoreCase) != id.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase))
return new ModInfoModel($"The value '{id}' isn't a valid GitHub mod ID, must be a username and project name like 'Pathoschild/LookupAnything'.");
// fetch info
try try
{ {
GitRelease release = await this.Client GitRelease release = await this.Client
@ -53,6 +59,10 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories
.As<GitRelease>(); .As<GitRelease>();
return new ModInfoModel(id, this.NormaliseVersion(release.Tag), $"https://github.com/{id}/releases"); return new ModInfoModel(id, this.NormaliseVersion(release.Tag), $"https://github.com/{id}/releases");
} }
catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound)
{
return new ModInfoModel("Found no mod with this ID.");
}
catch (Exception ex) catch (Exception ex)
{ {
return new ModInfoModel(ex.ToString()); return new ModInfoModel(ex.ToString());

View File

@ -38,6 +38,11 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories
/// <param name="id">The mod ID in this repository.</param> /// <param name="id">The mod ID in this repository.</param>
public override async Task<ModInfoModel> GetModInfoAsync(string id) public override async Task<ModInfoModel> GetModInfoAsync(string id)
{ {
// validate ID format
if (!uint.TryParse(id, out uint _))
return new ModInfoModel($"The value '{id}' isn't a valid Nexus mod ID, must be an integer ID.");
// fetch info
try try
{ {
NexusResponseModel response = await this.Client NexusResponseModel response = await this.Client