From 055fd5a940c55acc04e5585f9b097095141df478 Mon Sep 17 00:00:00 2001 From: Toastie Date: Fri, 27 Dec 2024 03:39:00 +1300 Subject: [PATCH] Improved error handling during template validation --- Commands/InterviewTemplateCommands.cs | 58 ++++++----- Interviews/Interview.cs | 113 +++++++++++----------- Interviews/interview_template.schema.json | 6 +- Utilities.cs | 12 +++ 4 files changed, 104 insertions(+), 85 deletions(-) diff --git a/Commands/InterviewTemplateCommands.cs b/Commands/InterviewTemplateCommands.cs index 9a21210..e628dfd 100644 --- a/Commands/InterviewTemplateCommands.cs +++ b/Commands/InterviewTemplateCommands.cs @@ -38,7 +38,7 @@ public class InterviewTemplateCommands return; } - if (!Database.TryGetCategory(category.Id, out Database.Category categoryData)) + if (!Database.TryGetCategory(category.Id, out Database.Category _)) { await command.RespondAsync(new DiscordEmbedBuilder { @@ -99,17 +99,15 @@ public class InterviewTemplateCommands return; } + Stream stream = await new HttpClient().GetStreamAsync(file.Url); try { - List errors = []; - - Stream stream = await new HttpClient().GetStreamAsync(file.Url); JSchemaValidatingReader validatingReader = new(new JsonTextReader(new StreamReader(stream))); validatingReader.Schema = JSchema.Parse(jsonSchema); // The schema seems to throw an additional error with incorrect information if an invalid parameter is included // in the template. Throw here in order to only show the first correct error to the user, also skips unnecessary validation further down. - validatingReader.ValidationEventHandler += (o, a) => throw new JsonException(a.Message); + validatingReader.ValidationEventHandler += (_, a) => throw new JsonException(a.Message); JsonSerializer serializer = new(); Template template = serializer.Deserialize