Fix IndexOutOfRangeException to use default message instead of "index"#120881
Fix IndexOutOfRangeException to use default message instead of "index"#120881stephentoub merged 4 commits intomainfrom
Conversation
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
This PR fixes IndexOutOfRangeException messages in the logging infrastructure to provide user-friendly error messages instead of the unhelpful parameter name "index". When accessing invalid indices in LoggerMessage.LogValues collections, the code was incorrectly using nameof(index) as the exception message.
Key changes:
- Replaced
new IndexOutOfRangeException(nameof(index))with parameterless constructor - Updated all LogValues variants (0-6 parameters) and the source generator template
- Added comprehensive test coverage to verify proper exception messages
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| LoggerMessage.cs | Fixed 7 occurrences across different generic LogValues<T> classes to use parameterless constructor |
| LoggerMessageGenerator.Emitter.cs | Updated source generator template to emit correct exception constructor |
| Test baseline files (3) | Updated generated code baselines to reflect new exception constructor usage |
| LoggerMessageTest.cs | Added comprehensive test verifying exception messages for all LogValues variants |
|
@copilot, it looks like there are some more occurrences of this same pattern across the repo. Please search for and fix all other occurrences similarly. |
…oss the repo Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Fixed all remaining occurrences. Found and updated 5 additional files:
All tests pass. Changes committed in 39d492d. |
Summary
Multiple classes across the repository were throwing
IndexOutOfRangeExceptionwithnameof(index)as the message parameter, resulting in an unhelpful error message of just "index" instead of the standard .NET exception message.Changes
This PR updates all occurrences to use the parameterless
IndexOutOfRangeException()constructor, which provides the default, user-friendly message: "Index was outside the bounds of the array."Before:
After:
Files Modified
Logging-related files:
LogValues<T>classesOther components:
Testing
LogValues_OutOfRangeAccess_ThrowsIndexOutOfRangeExceptionWithDefaultMessagethat validates the exception message for all LogValues variants (0-6 parameters)This is a minimal change that maintains backward compatibility (still throws
IndexOutOfRangeException) while significantly improving the developer experience with a clear, descriptive error message across the entire codebase.💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.