Skip to content

Commit

Permalink
Method name is added to exception message. (#787)
Browse files Browse the repository at this point in the history
### Motivation, Context and Description
This PR adds function/method name into exception message that is thrown
if a native function, marked with SKFunctionInputAttribute, doesn't have
an input parameter of string type. The method name can be useful for
debugging and troubleshooting.
  • Loading branch information
SergeyMenshykh committed May 6, 2023
1 parent 3e1d13e commit 22d12cb
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ private static MethodDetails GetMethodDetails(MethodInfo methodSignature, object

if (!result.HasSkFunctionAttribute || skFunctionAttribute == null)
{
log?.LogTrace("Method {0} doesn't have SKFunctionAttribute", result.Name);
log?.LogTrace("Method '{0}' doesn't have '{1}' attribute.", result.Name, typeof(SKFunctionAttribute).Name);
return result;
}

Expand Down Expand Up @@ -554,9 +554,8 @@ private static MethodDetails GetMethodDetails(MethodInfo methodSignature, object
else if (skMainParam != null)
{
// The developer used [SKFunctionInput] on a function that doesn't support a string input
throw new KernelException(
KernelException.ErrorCodes.InvalidFunctionDescription,
"The function doesn't have a string parameter, do not use " + typeof(SKFunctionInputAttribute));
var message = $"The method '{result.Name}' doesn't have a string parameter, do not use '{typeof(SKFunctionInputAttribute).Name}' attribute.";
throw new KernelException(KernelException.ErrorCodes.InvalidFunctionDescription, message);
}

// Handle named arg passed via the SKContext object
Expand All @@ -570,7 +569,7 @@ private static MethodDetails GetMethodDetails(MethodInfo methodSignature, object

result.Description = skFunctionAttribute.Description ?? "";

log?.LogTrace("Method {0} found", result.Name);
log?.LogTrace("Method '{0}' found.", result.Name);

return result;
}
Expand Down Expand Up @@ -678,13 +677,13 @@ private static (DelegateTypes type, Delegate function, bool hasStringParam) GetD
{
throw new KernelException(
KernelException.ErrorCodes.FunctionTypeNotSupported,
$"Function {method.Name} has an invalid signature 'Func<SKContext, SKContext>'. " +
$"Function '{method.Name}' has an invalid signature 'Func<SKContext, SKContext>'. " +
"Please use 'Func<SKContext, Task<SKContext>>' instead.");
}

throw new KernelException(
KernelException.ErrorCodes.FunctionTypeNotSupported,
$"Function {method.Name} has an invalid signature not supported by the kernel");
$"Function '{method.Name}' has an invalid signature not supported by the kernel.");
}

[SuppressMessage("Maintainability", "CA1508:Avoid dead conditional code", Justification = "Delegate.CreateDelegate result can be null")]
Expand Down

0 comments on commit 22d12cb

Please sign in to comment.