semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

Throw ArgumentNullExceptions for null arguments

Open stephentoub opened this issue 1 year ago • 1 comments

Motivation and Context

Use ArgumentNullException as devs expect, and avoid exception-related work when not throwing.

Description

.NET developers expect that passing null erroneously to a method will result in an ArgumentNullException, but currently it's resulting in a custom ValidationException.

There are also places where string interpolation is being used to create the error message; the work and allocation associated with that interpolation is going to happen regardless of whether the exception is thrown or not.

Contribution Checklist

  • [x] The code builds clean without any errors or warnings
  • [x] The PR follows SK Contribution Guidelines (https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
  • [x] The code follows the .NET coding conventions (https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions) verified with dotnet format
  • [x] All unit tests pass, and I have added new tests where possible
  • [ ] I didn't break anyone :smile:

stephentoub avatar Apr 21 '23 03:04 stephentoub

LGTM.

💡 A code weaver like Fody.NullGuard or a Roslyn code generator might be helpful in replacing all those runtime null checks.

MovGP0 avatar Apr 21 '23 13:04 MovGP0

@stephentoub looks like there are some conflicts that need to be resolved before merging.

lemillermicrosoft avatar Apr 27 '23 13:04 lemillermicrosoft

looks like there are some conflicts that need to be resolved before merging.

Done. Thanks.

stephentoub avatar Apr 27 '23 18:04 stephentoub