diff --git a/dotnet/samples/KernelSyntaxExamples/Example12_SequentialPlanner.cs b/dotnet/samples/KernelSyntaxExamples/Example12_SequentialPlanner.cs index e9e09fb541ab..a61a9c7b4a40 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example12_SequentialPlanner.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example12_SequentialPlanner.cs @@ -270,7 +270,7 @@ private static async Task ExecutePlanAsync( Console.WriteLine(plan.State.ToString()); } } - catch (KernelException e) + catch (SKException e) { Console.WriteLine("Step - Execution failed:"); Console.WriteLine(e.Message); diff --git a/dotnet/src/Extensions/Planning.SequentialPlanner/SKContextSequentialPlannerExtensions.cs b/dotnet/src/Extensions/Planning.SequentialPlanner/SKContextSequentialPlannerExtensions.cs index ad3ea7b5bbd4..dccc40bb2c10 100644 --- a/dotnet/src/Extensions/Planning.SequentialPlanner/SKContextSequentialPlannerExtensions.cs +++ b/dotnet/src/Extensions/Planning.SequentialPlanner/SKContextSequentialPlannerExtensions.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using Microsoft.SemanticKernel.Diagnostics; using Microsoft.SemanticKernel.Memory; using Microsoft.SemanticKernel.Planning.Sequential; using Microsoft.SemanticKernel.SkillDefinition; @@ -60,9 +61,7 @@ public static async Task> GetAvailableFunctions if (context.Skills == null) { - throw new KernelException( - KernelException.ErrorCodes.SkillCollectionNotSet, - "Skill collection not found in the context"); + throw new SKException("Skill collection not found in the context"); } var functionsView = context.Skills.GetFunctionsView(); diff --git a/dotnet/src/InternalUtilities/src/Diagnostics/Verify.cs b/dotnet/src/InternalUtilities/src/Diagnostics/Verify.cs index 79ad183bd094..c179ec620955 100644 --- a/dotnet/src/InternalUtilities/src/Diagnostics/Verify.cs +++ b/dotnet/src/InternalUtilities/src/Diagnostics/Verify.cs @@ -108,9 +108,7 @@ internal static void ParametersUniqueness(IList parameters) if (!seen.Add(p.Name)) { - throw new KernelException( - KernelException.ErrorCodes.InvalidFunctionDescription, - $"The function has two or more parameters with the same name '{p.Name}'"); + throw new SKException($"The function has two or more parameters with the same name '{p.Name}'"); } } } @@ -118,9 +116,7 @@ internal static void ParametersUniqueness(IList parameters) [DoesNotReturn] private static void ThrowInvalidName(string kind, string name) => - throw new KernelException( - KernelException.ErrorCodes.InvalidFunctionDescription, - $"A {kind} can contain only ASCII letters, digits, and underscores: '{name}' is not a valid name."); + throw new SKException($"A {kind} can contain only ASCII letters, digits, and underscores: '{name}' is not a valid name."); [DoesNotReturn] internal static void ThrowArgumentNullException(string? paramName) => diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionServiceExtensions.cs b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionServiceExtensions.cs index f6edaf5a2ee0..a7b2f32ecf8a 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionServiceExtensions.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionServiceExtensions.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. using Microsoft.SemanticKernel.AI.ChatCompletion; +using Microsoft.SemanticKernel.Diagnostics; using Microsoft.SemanticKernel.Services; // Use base namespace for better discoverability and to avoid conflicts with other extensions. @@ -17,11 +18,11 @@ public static class ChatCompletionServiceExtensions /// The service provider. /// Optional identifier of the desired service. /// The completion service id matching the given id or the default. - /// Thrown when no suitable service is found. + /// Thrown when no suitable service is found. public static IChatCompletion GetChatCompletionService( this IAIServiceProvider services, string? serviceId = null) => services.GetService(serviceId) - ?? throw new KernelException(KernelException.ErrorCodes.ServiceNotFound, "Chat completion service not found"); + ?? throw new SKException("Chat completion service not found"); /// /// Returns true if a exist with the specified ID. diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/Embeddings/TextEmbeddingServiceExtensions.cs b/dotnet/src/SemanticKernel.Abstractions/AI/Embeddings/TextEmbeddingServiceExtensions.cs index 613161f68002..76e3926b5e60 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/Embeddings/TextEmbeddingServiceExtensions.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/Embeddings/TextEmbeddingServiceExtensions.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. using Microsoft.SemanticKernel.AI.Embeddings; +using Microsoft.SemanticKernel.Diagnostics; using Microsoft.SemanticKernel.Services; // Use base namespace for better discoverability and to avoid conflicts with other extensions. @@ -17,12 +18,12 @@ public static class TextEmbeddingServiceExtensions /// The service provider. /// Optional identifier of the desired service. /// The embedding service matching the given id or the default service. - /// Thrown when no suitable service is found. + /// Thrown when no suitable service is found. public static ITextEmbeddingGeneration GetTextEmbeddingService( this IAIServiceProvider services, string? serviceId = null) => services.GetService(serviceId) - ?? throw new KernelException(KernelException.ErrorCodes.ServiceNotFound, "Text embedding service not available"); + ?? throw new SKException("Text embedding service not available"); /// /// Returns true if a exist with the specified ID. diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/ImageGeneration/ImageGenerationServiceExtensions.cs b/dotnet/src/SemanticKernel.Abstractions/AI/ImageGeneration/ImageGenerationServiceExtensions.cs index 17541b3e3aca..cc1f335caaa2 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/ImageGeneration/ImageGenerationServiceExtensions.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/ImageGeneration/ImageGenerationServiceExtensions.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. using Microsoft.SemanticKernel.AI.ImageGeneration; +using Microsoft.SemanticKernel.Diagnostics; using Microsoft.SemanticKernel.Services; // Use base namespace for better discoverability and to avoid conflicts with other extensions. @@ -17,11 +18,11 @@ public static class ImageGenerationServiceExtensions /// The service provider. /// Optional identifier of the desired service. /// The id matching the given id or the default. - /// Thrown when no suitable service is found. + /// Thrown when no suitable service is found. public static IImageGeneration GetImageGenerationService( this IAIServiceProvider services, string? serviceId = null) => services.GetService(serviceId) - ?? throw new KernelException(KernelException.ErrorCodes.ServiceNotFound, "Image generation service not found"); + ?? throw new SKException("Image generation service not found"); /// /// Returns true if a exist with the specified ID. diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionServiceExtensions.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionServiceExtensions.cs index 5a0a9308af28..bf5816db1d22 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionServiceExtensions.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionServiceExtensions.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. using Microsoft.SemanticKernel.AI.TextCompletion; +using Microsoft.SemanticKernel.Diagnostics; using Microsoft.SemanticKernel.Services; // Use base namespace for better discoverability and to avoid conflicts with other extensions. @@ -17,11 +18,11 @@ public static class TextCompletionServiceExtensions /// The service provider. /// Optional identifier of the desired service. /// The text completion service id matching the given ID or the default. - /// Thrown when no suitable service is found. + /// Thrown when no suitable service is found. public static ITextCompletion GetTextCompletionServiceOrDefault( this IAIServiceProvider services, string? serviceId = null) => services.GetService(serviceId) - ?? throw new KernelException(KernelException.ErrorCodes.ServiceNotFound, "Text completion service not found"); + ?? throw new SKException("Text completion service not found"); /// /// Returns true if a exist with the specified ID. diff --git a/dotnet/src/SemanticKernel.Abstractions/KernelException.cs b/dotnet/src/SemanticKernel.Abstractions/KernelException.cs deleted file mode 100644 index 681fc4d81c71..000000000000 --- a/dotnet/src/SemanticKernel.Abstractions/KernelException.cs +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. - -using System; -using Microsoft.SemanticKernel.Diagnostics; - -namespace Microsoft.SemanticKernel; - -#pragma warning disable RCS1194 // Implement exception constructors - -/// -/// Exception thrown for errors related to kernel logic. -/// -public class KernelException : SKException -{ - /// - /// Initializes a new instance of the class with a provided error code and message. - /// - /// The error code. - /// The exception message. - public KernelException(ErrorCodes errorCode, string? message) - : this(errorCode, message, innerException: null) - { - } - - /// - /// Initializes a new instance of the class with a provided error code, message, and inner exception. - /// - /// The error code. - /// A string that describes the error. - /// The exception that is the cause of the current exception. - public KernelException(ErrorCodes errorCode, string? message = null, Exception? innerException = null) - : base(GetDefaultMessage(errorCode, message), innerException) - { - this.ErrorCode = errorCode; - } - - /// - /// Gets the error code for this exception. - /// - public ErrorCodes ErrorCode { get; } - - /// Translate the error code into a default message. - /// The error code. - /// Default error message if nothing available. - private static string GetDefaultMessage(ErrorCodes errorCode, string? defaultMessage) - { - string description = errorCode switch - { - ErrorCodes.InvalidFunctionDescription => "Invalid function description", - ErrorCodes.FunctionOverloadNotSupported => "Function overload not supported", - ErrorCodes.FunctionNotAvailable => "Function not available", - ErrorCodes.FunctionTypeNotSupported => "Function type not supported", - ErrorCodes.InvalidFunctionType => "Invalid function type", - ErrorCodes.InvalidServiceConfiguration => "Invalid service configuration", - ErrorCodes.ServiceNotFound => "Service not found", - ErrorCodes.SkillCollectionNotSet => "Skill collection not set", - ErrorCodes.FunctionInvokeError => "Function invoke error", - _ => $"Unknown error ({errorCode:G})", - }; - - return defaultMessage is not null ? $"{description}: {defaultMessage}" : description; - } - - /// - /// Semantic kernel error codes. - /// - public enum ErrorCodes - { - /// - /// Unknown error. - /// - UnknownError = -1, - - /// - /// Invalid function description. - /// - InvalidFunctionDescription, - - /// - /// Function overload not supported. - /// - FunctionOverloadNotSupported, - - /// - /// Function not available. - /// - FunctionNotAvailable, - - /// - /// Function type not supported. - /// - FunctionTypeNotSupported, - - /// - /// Invalid function type. - /// - InvalidFunctionType, - - /// - /// Invalid service configuration. - /// - InvalidServiceConfiguration, - - /// - /// Service not found. - /// - ServiceNotFound, - - /// - /// Skill collection not set. - /// - SkillCollectionNotSet, - - /// - /// Represents an error that occurs when invoking a function. - /// - FunctionInvokeError, - } -} diff --git a/dotnet/src/SemanticKernel.Abstractions/Orchestration/SKContext.cs b/dotnet/src/SemanticKernel.Abstractions/Orchestration/SKContext.cs index f2027dc2d842..b2addee969e7 100644 --- a/dotnet/src/SemanticKernel.Abstractions/Orchestration/SKContext.cs +++ b/dotnet/src/SemanticKernel.Abstractions/Orchestration/SKContext.cs @@ -7,6 +7,7 @@ using System.Threading; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.SemanticKernel.Diagnostics; using Microsoft.SemanticKernel.Memory; using Microsoft.SemanticKernel.SkillDefinition; @@ -115,9 +116,7 @@ public ISKFunction Func(string skillName, string functionName) { if (this.Skills is null) { - throw new KernelException( - KernelException.ErrorCodes.SkillCollectionNotSet, - "Skill collection not found in the context"); + throw new SKException("Skill collection not found in the context"); } return this.Skills.GetFunction(skillName, functionName); diff --git a/dotnet/src/SemanticKernel.Abstractions/SkillDefinition/IReadOnlySkillCollection.cs b/dotnet/src/SemanticKernel.Abstractions/SkillDefinition/IReadOnlySkillCollection.cs index b33be49568b4..df5dde768194 100644 --- a/dotnet/src/SemanticKernel.Abstractions/SkillDefinition/IReadOnlySkillCollection.cs +++ b/dotnet/src/SemanticKernel.Abstractions/SkillDefinition/IReadOnlySkillCollection.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. using System.Diagnostics.CodeAnalysis; +using Microsoft.SemanticKernel.Diagnostics; namespace Microsoft.SemanticKernel.SkillDefinition; @@ -15,7 +16,7 @@ public interface IReadOnlySkillCollection /// /// The name of the function to retrieve. /// The function retrieved from the collection. - /// The specified function could not be found in the collection. + /// The specified function could not be found in the collection. ISKFunction GetFunction(string functionName); /// @@ -24,7 +25,7 @@ public interface IReadOnlySkillCollection /// The name of the skill with which the function is associated. /// The name of the function to retrieve. /// The function retrieved from the collection. - /// The specified function could not be found in the collection. + /// The specified function could not be found in the collection. ISKFunction GetFunction(string skillName, string functionName); /// diff --git a/dotnet/src/SemanticKernel.Abstractions/SkillDefinition/NullReadOnlySkillCollection.cs b/dotnet/src/SemanticKernel.Abstractions/SkillDefinition/NullReadOnlySkillCollection.cs index 2b833e9b4151..b077ce286bf9 100644 --- a/dotnet/src/SemanticKernel.Abstractions/SkillDefinition/NullReadOnlySkillCollection.cs +++ b/dotnet/src/SemanticKernel.Abstractions/SkillDefinition/NullReadOnlySkillCollection.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using Microsoft.SemanticKernel.Diagnostics; namespace Microsoft.SemanticKernel.SkillDefinition; @@ -44,16 +45,12 @@ private NullReadOnlySkillCollection() [DoesNotReturn] private static ISKFunction ThrowFunctionNotAvailable(string skillName, string functionName) { - throw new KernelException( - KernelException.ErrorCodes.FunctionNotAvailable, - $"Function not available: {skillName}.{functionName}"); + throw new SKException($"Function not available: {skillName}.{functionName}"); } [DoesNotReturn] private static ISKFunction ThrowFunctionNotAvailable(string functionName) { - throw new KernelException( - KernelException.ErrorCodes.FunctionNotAvailable, - $"Function not available: {functionName}"); + throw new SKException($"Function not available: {functionName}"); } } diff --git a/dotnet/src/SemanticKernel.UnitTests/KernelExceptionTests.cs b/dotnet/src/SemanticKernel.UnitTests/KernelExceptionTests.cs deleted file mode 100644 index 3bb684cda1ec..000000000000 --- a/dotnet/src/SemanticKernel.UnitTests/KernelExceptionTests.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. - -using System; -using Microsoft.SemanticKernel; -using Xunit; - -namespace SemanticKernel.UnitTests; - -public class KernelExceptionTests -{ - [Fact] - public void ItRoundtripsArgsToErrorCodeCtor() - { - // Arrange - var e = new KernelException(KernelException.ErrorCodes.FunctionNotAvailable); - - // Assert - Assert.Equal(KernelException.ErrorCodes.FunctionNotAvailable, e.ErrorCode); - Assert.Contains("Function not available", e.Message, StringComparison.Ordinal); - Assert.Null(e.InnerException); - } - - [Fact] - public void ItRoundtripsArgsToErrorCodeMessageCtor() - { - // Arrange - const string Message = "this is a test"; - var e = new KernelException(KernelException.ErrorCodes.FunctionNotAvailable, Message); - - // Assert - Assert.Equal(KernelException.ErrorCodes.FunctionNotAvailable, e.ErrorCode); - Assert.Contains("Function not available", e.Message, StringComparison.Ordinal); - Assert.Contains(Message, e.Message, StringComparison.Ordinal); - Assert.Null(e.InnerException); - } - - [Fact] - public void ItRoundtripsArgsToErrorCodeMessageExceptionCtor() - { - // Arrange - const string Message = "this is a test"; - var inner = new FormatException(); - var e = new KernelException(KernelException.ErrorCodes.FunctionNotAvailable, Message, inner); - - // Assert - Assert.Equal(KernelException.ErrorCodes.FunctionNotAvailable, e.ErrorCode); - Assert.Contains("Function not available", e.Message, StringComparison.Ordinal); - Assert.Contains(Message, e.Message, StringComparison.Ordinal); - Assert.Same(inner, e.InnerException); - } - - [Fact] - public void ItAllowsNullMessageAndInnerExceptionInCtors() - { - // Arrange - var e = new KernelException(KernelException.ErrorCodes.FunctionNotAvailable, null, null); - - // Assert - Assert.Equal(KernelException.ErrorCodes.FunctionNotAvailable, e.ErrorCode); - Assert.Contains("Function not available", e.Message, StringComparison.Ordinal); - Assert.Null(e.InnerException); - } -} diff --git a/dotnet/src/SemanticKernel.UnitTests/Planning/PlanSerializationTests.cs b/dotnet/src/SemanticKernel.UnitTests/Planning/PlanSerializationTests.cs index 1101e8cf4802..6f35e2d06088 100644 --- a/dotnet/src/SemanticKernel.UnitTests/Planning/PlanSerializationTests.cs +++ b/dotnet/src/SemanticKernel.UnitTests/Planning/PlanSerializationTests.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Logging; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.AI.TextCompletion; +using Microsoft.SemanticKernel.Diagnostics; using Microsoft.SemanticKernel.Memory; using Microsoft.SemanticKernel.Orchestration; using Microsoft.SemanticKernel.Planning; @@ -584,7 +585,7 @@ public void DeserializeWithMissingFunctions(bool requireFunctions) if (requireFunctions) { // Act + Assert - Assert.Throws(() => Plan.FromJson(serializedPlan, returnContext)); + Assert.Throws(() => Plan.FromJson(serializedPlan, returnContext)); } else { diff --git a/dotnet/src/SemanticKernel.UnitTests/Planning/PlanTests.cs b/dotnet/src/SemanticKernel.UnitTests/Planning/PlanTests.cs index 88ea49845bd1..b2d29310850d 100644 --- a/dotnet/src/SemanticKernel.UnitTests/Planning/PlanTests.cs +++ b/dotnet/src/SemanticKernel.UnitTests/Planning/PlanTests.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Logging; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.AI.TextCompletion; +using Microsoft.SemanticKernel.Diagnostics; using Microsoft.SemanticKernel.Memory; using Microsoft.SemanticKernel.Orchestration; using Microsoft.SemanticKernel.Planning; @@ -413,7 +414,7 @@ public async Task StepExceptionIsThrownAsync() // Act var cv = new ContextVariables(planInput); - await Assert.ThrowsAsync(async () => await kernel.Object.StepAsync(cv, plan)); + await Assert.ThrowsAsync(async () => await kernel.Object.StepAsync(cv, plan)); mockFunction.Verify(x => x.InvokeAsync(It.IsAny(), null), Times.Once); } @@ -449,7 +450,7 @@ public async Task PlanStepExceptionIsThrownAsync() // Act var cv = new ContextVariables(planInput); - await Assert.ThrowsAsync(async () => await kernel.Object.StepAsync(cv, plan)); + await Assert.ThrowsAsync(async () => await kernel.Object.StepAsync(cv, plan)); mockFunction.Verify(x => x.InvokeAsync(It.IsAny(), null), Times.Once); } diff --git a/dotnet/src/SemanticKernel.UnitTests/SkillDefinition/SKFunctionTests3.cs b/dotnet/src/SemanticKernel.UnitTests/SkillDefinition/SKFunctionTests3.cs index 52ba2a65952c..8142a8349b36 100644 --- a/dotnet/src/SemanticKernel.UnitTests/SkillDefinition/SKFunctionTests3.cs +++ b/dotnet/src/SemanticKernel.UnitTests/SkillDefinition/SKFunctionTests3.cs @@ -7,6 +7,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.SemanticKernel; +using Microsoft.SemanticKernel.Diagnostics; using Microsoft.SemanticKernel.Orchestration; using Microsoft.SemanticKernel.SkillDefinition; using Xunit; @@ -67,7 +68,7 @@ public void ItThrowsForInvalidFunctions() { SKFunction.FromNativeMethod(method, instance, "skill"); } - catch (KernelException e) when (e.ErrorCode is KernelException.ErrorCodes.FunctionTypeNotSupported or KernelException.ErrorCodes.InvalidFunctionDescription) + catch (SKException) { count++; } diff --git a/dotnet/src/SemanticKernel/Kernel.cs b/dotnet/src/SemanticKernel/Kernel.cs index c0b9ae60069f..cab00c41164c 100644 --- a/dotnet/src/SemanticKernel/Kernel.cs +++ b/dotnet/src/SemanticKernel/Kernel.cs @@ -235,7 +235,7 @@ public T GetService(string? name = null) where T : IAIService return service; } - throw new KernelException(KernelException.ErrorCodes.ServiceNotFound, $"Service of type {typeof(T)} and name {name ?? ""} not registered."); + throw new SKException($"Service of type {typeof(T)} and name {name ?? ""} not registered."); } /// @@ -309,9 +309,7 @@ private static Dictionary ImportSkill(object skillInstance, ISKFunction function = SKFunction.FromNativeMethod(method, skillInstance, skillName, log); if (result.ContainsKey(function.Name)) { - throw new KernelException( - KernelException.ErrorCodes.FunctionOverloadNotSupported, - "Function overloads are not supported, please differentiate function names"); + throw new SKException("Function overloads are not supported, please differentiate function names"); } result.Add(function.Name, function); diff --git a/dotnet/src/SemanticKernel/Planning/Plan.cs b/dotnet/src/SemanticKernel/Planning/Plan.cs index 541fc2a38b49..90471a41dd72 100644 --- a/dotnet/src/SemanticKernel/Planning/Plan.cs +++ b/dotnet/src/SemanticKernel/Planning/Plan.cs @@ -11,6 +11,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.SemanticKernel.AI.TextCompletion; +using Microsoft.SemanticKernel.Diagnostics; using Microsoft.SemanticKernel.Memory; using Microsoft.SemanticKernel.Orchestration; using Microsoft.SemanticKernel.SkillDefinition; @@ -239,7 +240,7 @@ public Task RunNextStepAsync(IKernel kernel, ContextVariables variables, C /// /// Context to use /// The updated plan - /// If an error occurs while running the plan + /// If an error occurs while running the plan public async Task InvokeNextStepAsync(SKContext context) { if (this.HasNextStep) @@ -256,8 +257,7 @@ public async Task InvokeNextStepAsync(SKContext context) if (result.ErrorOccurred) { - throw new KernelException(KernelException.ErrorCodes.FunctionInvokeError, - $"Error occurred while running plan step: {result.LastErrorDescription}", result.LastException); + throw new SKException($"Error occurred while running plan step: {result.LastErrorDescription}", result.LastException); } #region Update State @@ -420,9 +420,7 @@ private static Plan SetAvailableFunctions(Plan plan, SKContext context, bool req { if (context.Skills == null) { - throw new KernelException( - KernelException.ErrorCodes.SkillCollectionNotSet, - "Skill collection not found in the context"); + throw new SKException("Skill collection not found in the context"); } if (context.Skills.TryGetFunction(plan.SkillName, plan.Name, out var skillFunction)) @@ -431,9 +429,7 @@ private static Plan SetAvailableFunctions(Plan plan, SKContext context, bool req } else if (requireFunctions) { - throw new KernelException( - KernelException.ErrorCodes.FunctionNotAvailable, - $"Function '{plan.SkillName}.{plan.Name}' not found in skill collection"); + throw new SKException($"Function '{plan.SkillName}.{plan.Name}' not found in skill collection"); } } else diff --git a/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs b/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs index 4c26ff23c443..aa1ba2795f38 100644 --- a/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs +++ b/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs @@ -369,15 +369,13 @@ internal SKFunction( /// /// Throw an exception if the function is not semantic, use this method when some logic makes sense only for semantic functions. /// - /// + /// private void VerifyIsSemantic() { if (this.IsSemantic) { return; } this._log.LogError("The function is not semantic"); - throw new KernelException( - KernelException.ErrorCodes.InvalidFunctionType, - "Invalid operation, the method requires a semantic function"); + throw new SKException("Invalid operation, the method requires a semantic function"); } private static MethodDetails GetMethodDetails( @@ -610,7 +608,7 @@ private static (Func, ParameterView?) GetParameterMarshalerD } // 4. Otherwise, fail. - throw new KernelException(KernelException.ErrorCodes.FunctionInvokeError, $"Missing value for parameter '{name}'"); + throw new SKException($"Missing value for parameter '{name}'"); object? Process(string value) { @@ -777,15 +775,13 @@ private static (Func, ParameterView?) GetParameterMarshalerD // Throws an exception if a result is found to be null unexpectedly static object ThrowIfNullResult(object? result) => result ?? - throw new KernelException(KernelException.ErrorCodes.FunctionInvokeError, "Function returned null unexpectedly."); + throw new SKException("Function returned null unexpectedly."); } /// Gets an exception that can be thrown indicating an invalid signature. [DoesNotReturn] private static Exception GetExceptionForInvalidSignature(MethodInfo method, string reason) => - throw new KernelException( - KernelException.ErrorCodes.FunctionTypeNotSupported, - $"Function '{method.Name}' is not supported by the kernel. {reason}"); + throw new SKException($"Function '{method.Name}' is not supported by the kernel. {reason}"); /// Throws an exception indicating an invalid SKFunction signature if the specified condition is not met. private static void ThrowForInvalidSignatureIf([DoesNotReturnIf(true)] bool condition, MethodInfo method, string reason) diff --git a/dotnet/src/SemanticKernel/SkillDefinition/SkillCollection.cs b/dotnet/src/SemanticKernel/SkillDefinition/SkillCollection.cs index da87c7ad88cd..9829c3353aea 100644 --- a/dotnet/src/SemanticKernel/SkillDefinition/SkillCollection.cs +++ b/dotnet/src/SemanticKernel/SkillDefinition/SkillCollection.cs @@ -111,9 +111,7 @@ public FunctionsView GetFunctionsView(bool includeSemantic = true, bool includeN private void ThrowFunctionNotAvailable(string skillName, string functionName) { this._log.LogError("Function not available: skill:{0} function:{1}", skillName, functionName); - throw new KernelException( - KernelException.ErrorCodes.FunctionNotAvailable, - $"Function not available {skillName}.{functionName}"); + throw new SKException($"Function not available {skillName}.{functionName}"); } private readonly ILogger _log; diff --git a/dotnet/src/SemanticKernel/TemplateEngine/Blocks/CodeBlock.cs b/dotnet/src/SemanticKernel/TemplateEngine/Blocks/CodeBlock.cs index 3886d2328626..59290221e38e 100644 --- a/dotnet/src/SemanticKernel/TemplateEngine/Blocks/CodeBlock.cs +++ b/dotnet/src/SemanticKernel/TemplateEngine/Blocks/CodeBlock.cs @@ -101,9 +101,7 @@ private async Task RenderFunctionCallAsync(FunctionIdBlock fBlock, SKCon { if (context.Skills == null) { - throw new KernelException( - KernelException.ErrorCodes.SkillCollectionNotSet, - "Skill collection not found in the context"); + throw new SKException("Skill collection not found in the context"); } if (!this.GetFunctionFromSkillCollection(context.Skills!, fBlock, out ISKFunction? function)) diff --git a/dotnet/src/Skills/Skills.OpenAPI/Extensions/KernelOpenApiExtensions.cs b/dotnet/src/Skills/Skills.OpenAPI/Extensions/KernelOpenApiExtensions.cs index ed63cdbb14de..a007370ec2f6 100644 --- a/dotnet/src/Skills/Skills.OpenAPI/Extensions/KernelOpenApiExtensions.cs +++ b/dotnet/src/Skills/Skills.OpenAPI/Extensions/KernelOpenApiExtensions.cs @@ -320,7 +320,7 @@ private static string ConvertOperationIdToValidFunctionName(string operationId, Verify.ValidFunctionName(operationId); return operationId; } - catch (KernelException) + catch (SKException) { } diff --git a/dotnet/src/Skills/Skills.UnitTests/MsGraph/CalendarSkillTests.cs b/dotnet/src/Skills/Skills.UnitTests/MsGraph/CalendarSkillTests.cs index eaaa5be61c67..50ec69a6b789 100644 --- a/dotnet/src/Skills/Skills.UnitTests/MsGraph/CalendarSkillTests.cs +++ b/dotnet/src/Skills/Skills.UnitTests/MsGraph/CalendarSkillTests.cs @@ -4,7 +4,7 @@ using System.Globalization; using System.Threading; using System.Threading.Tasks; -using Microsoft.SemanticKernel; +using Microsoft.SemanticKernel.Diagnostics; using Microsoft.SemanticKernel.Orchestration; using Microsoft.SemanticKernel.Skills.MsGraph; using Microsoft.SemanticKernel.Skills.MsGraph.Models; @@ -201,8 +201,7 @@ public async Task AddEventAsyncWithoutStartFailsAsync() // Assert Assert.True(context.ErrorOccurred); - KernelException e = Assert.IsType(context.LastException); - Assert.Equal(KernelException.ErrorCodes.FunctionInvokeError, e.ErrorCode); + Assert.IsType(context.LastException); } [Fact] @@ -229,8 +228,7 @@ public async Task AddEventAsyncWithoutEndFailsAsync() // Assert Assert.True(context.ErrorOccurred); - KernelException e = Assert.IsType(context.LastException); - Assert.Equal(KernelException.ErrorCodes.FunctionInvokeError, e.ErrorCode); + Assert.IsType(context.LastException); } [Fact]