Throw more readable exceptions if deserialization fails for Mangaconnectors.

#249
This commit is contained in:
Glax 2024-09-16 18:32:34 +02:00
parent 895128a462
commit 96f3dbce65

View File

@ -1,4 +1,6 @@
using Newtonsoft.Json; using System.Data;
using System.Diagnostics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
namespace Tranga.MangaConnectors; namespace Tranga.MangaConnectors;
@ -22,29 +24,22 @@ public class MangaConnectorJsonConverter : JsonConverter
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{ {
JObject jo = JObject.Load(reader); JObject jo = JObject.Load(reader);
switch (jo.GetValue("name")!.Value<string>()!) string? connectorName = jo.Value<string>("name");
if (connectorName is null)
throw new ConstraintException("Name can not be null.");
return connectorName switch
{ {
case "MangaDex": "MangaDex" => this._connectors.First(c => c is MangaDex),
return this._connectors.First(c => c is MangaDex); "Manganato" => this._connectors.First(c => c is Manganato),
case "Manganato": "MangaKatana" => this._connectors.First(c => c is MangaKatana),
return this._connectors.First(c => c is Manganato); "Mangasee" => this._connectors.First(c => c is Mangasee),
case "MangaKatana": "Mangaworld" => this._connectors.First(c => c is Mangaworld),
return this._connectors.First(c => c is MangaKatana); "Bato" => this._connectors.First(c => c is Bato),
case "Mangasee": "Manga4Life" => this._connectors.First(c => c is MangaLife),
return this._connectors.First(c => c is Mangasee); "ManhuaPlus" => this._connectors.First(c => c is ManhuaPlus),
case "Mangaworld": "MangaHere" => this._connectors.First(c => c is MangaHere),
return this._connectors.First(c => c is Mangaworld); _ => throw new UnreachableException($"Could not find Connector with name {connectorName}")
case "Bato": };
return this._connectors.First(c => c is Bato);
case "Manga4Life":
return this._connectors.First(c => c is MangaLife);
case "ManhuaPlus":
return this._connectors.First(c => c is ManhuaPlus);
case "MangaHere":
return this._connectors.First(c => c is MangaHere);
}
throw new Exception();
} }
public override bool CanWrite => false; public override bool CanWrite => false;