From fcd66dfe184f75728435377902e52edc34f2a222 Mon Sep 17 00:00:00 2001 From: Amruth Baskar Date: Fri, 26 Jul 2024 15:33:10 -0700 Subject: [PATCH 1/2] Add custom exception for errors from Yaml Validator Library --- src/YamlValidator/SchemaLoader.cs | 4 ++-- src/YamlValidator/ValidatorError.cs | 4 ++-- src/YamlValidator/YamlValidatorLibraryException.cs | 12 ++++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 src/YamlValidator/YamlValidatorLibraryException.cs diff --git a/src/YamlValidator/SchemaLoader.cs b/src/YamlValidator/SchemaLoader.cs index 540a4685..85e3e4fc 100644 --- a/src/YamlValidator/SchemaLoader.cs +++ b/src/YamlValidator/SchemaLoader.cs @@ -22,7 +22,7 @@ public JsonSchema Load() var assemblyName = assembly.GetName().Name; if (fileStream == null) { - throw new IOException($"Resource {file} could not found in assembly {assemblyName}"); + throw new YamlValidatorLibraryException($"The schema could not be loaded from assembly."); } using var streamReader = new StreamReader(fileStream); var jsonSchemaString = streamReader.ReadToEnd(); @@ -45,7 +45,7 @@ public JsonSchema Load() } if (node == null) { - throw new InvalidDataException("Schema was not able to be read into memory"); + throw new YamlValidatorLibraryException("The schema could not be serialized from the assembly."); } return node; } diff --git a/src/YamlValidator/ValidatorError.cs b/src/YamlValidator/ValidatorError.cs index 32f9b5ef..f86b370a 100644 --- a/src/YamlValidator/ValidatorError.cs +++ b/src/YamlValidator/ValidatorError.cs @@ -31,9 +31,9 @@ public override string ToString() foreach (var error in Errors) { var errType = string.IsNullOrEmpty(error.Key) ? "Error" : error.Key; - errString += $"\t{errType}: {error.Value}\n"; + errString += $"\t\t- {errType}: {error.Value}\n"; } } - return $"InstanceLocation: {InstanceLocation}\nSchemaPath: {SchemaPath}\nErrors:\n{errString}"; + return $"\t- InstanceLocation: {InstanceLocation}\n\t- SchemaPath: {SchemaPath}\n\t- Errors:\n{errString}"; } } diff --git a/src/YamlValidator/YamlValidatorLibraryException.cs b/src/YamlValidator/YamlValidatorLibraryException.cs new file mode 100644 index 00000000..bf58c85f --- /dev/null +++ b/src/YamlValidator/YamlValidatorLibraryException.cs @@ -0,0 +1,12 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator; +public class YamlValidatorLibraryException : Exception +{ + public string Reason { get; } + public YamlValidatorLibraryException(string reason) + { + Reason = reason; + } +} From bfb647d428f3f5f3be09bb8ca0a5b95d4c57a243 Mon Sep 17 00:00:00 2001 From: Amruth Baskar Date: Mon, 29 Jul 2024 08:52:42 -0700 Subject: [PATCH 2/2] use message property from base class exception --- src/YamlValidator/YamlValidatorLibraryException.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/YamlValidator/YamlValidatorLibraryException.cs b/src/YamlValidator/YamlValidatorLibraryException.cs index bf58c85f..8638cd95 100644 --- a/src/YamlValidator/YamlValidatorLibraryException.cs +++ b/src/YamlValidator/YamlValidatorLibraryException.cs @@ -4,9 +4,7 @@ namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator; public class YamlValidatorLibraryException : Exception { - public string Reason { get; } - public YamlValidatorLibraryException(string reason) + public YamlValidatorLibraryException(string reason) : base(message: reason) { - Reason = reason; } }