Added error catching for FromJSON to catch NexusAPI error responses

This commit is contained in:
erri120 2019-12-16 14:00:22 +01:00
parent 720f17b216
commit ed1297b71d
No known key found for this signature in database
GPG Key ID: A8C0A18D8D4D3135

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;
}
}
}