Merge pull request #282 from erri120/fromjson-catch

Added error catching for FromJSON to catch NexusAPI error responses
This commit is contained in:
Timothy Baldridge 2019-12-16 13:31:41 -07:00 committed by GitHub
commit 0e9e57d3e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -394,11 +394,23 @@ namespace Wabbajack.Common
return JsonConvert.DeserializeObject<T>(data,
new JsonSerializerSettings {TypeNameHandling = handling, TypeNameAssemblyFormatHandling = format});
}
public static T FromJSON<T>(this Stream data)
{
var s = Encoding.UTF8.GetString(data.ReadAll());
return JsonConvert.DeserializeObject<T>(s,
new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
try
{
return JsonConvert.DeserializeObject<T>(s,
new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.Auto});
}
catch (JsonSerializationException)
{
var error = JsonConvert.DeserializeObject<NexusErrorResponse>(s,
new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.Auto});
if (error != null)
Log($"Exception while deserializing\nError code: {error.code}\nError message: {error.message}");
throw;
}
}
public static bool FileExists(this string filename)
@ -958,6 +970,10 @@ namespace Wabbajack.Common
return path.ToLower().TrimEnd('\\').StartsWith(parent.ToLower().TrimEnd('\\') + "\\");
}
public class NexusErrorResponse
{
public int code;
public string message;
}
}
}