FileConfigurationProvider: Handle and forward IO exceptions to OnLoadException#126093
FileConfigurationProvider: Handle and forward IO exceptions to OnLoadException#126093mrek-msft wants to merge 30 commits intodotnet:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Unifies FileConfigurationProvider failure handling so IO failures during file open are handled the same way as parsing failures with respect to OnLoadException (including on reload-on-change), preventing unobserved/unhandled exceptions like the one reported in #113964.
Changes:
- Moves stream opening (
OpenRead) inside the exception-handling region so IO errors are routed throughHandleException/OnLoadException. - Adds an inner
try/catchto wrap exceptions thrown byLoad(stream)in anInvalidDataExceptionwith the standard “Failed to load…” message. - Adjusts the outer catch to forward the exception through
HandleException(enabling ignore behavior on reload).
...libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationProvider.cs
Outdated
Show resolved
Hide resolved
...libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationProvider.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/DisposableFileSystem.cs
Outdated
Show resolved
Hide resolved
...libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationProvider.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs
Show resolved
Hide resolved
|
Tagging subscribers to this area: @dotnet/area-extensions-filesystem |
src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/DisposableFileSystem.cs
Show resolved
Hide resolved
...libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationProvider.cs
Show resolved
Hide resolved
...libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationProvider.cs
Outdated
Show resolved
Hide resolved
...libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationProvider.cs
Outdated
Show resolved
Hide resolved
|
Nit: the documentation of
Since it can now contain exceptions that weren't literally thrown from the
|
I updated it and agree that comment is better, but note that even after changes, all exceptions come from |
rosebyte
left a comment
There was a problem hiding this comment.
I like the direction of the PR, only left couple comments.
|
@copilot please invoke the code-review skill and post the analysis/results as a comment on this PR. |
… forwarding to OnLoadException Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/42d4769b-0756-4ab1-aaae-f73cf4cfaa3d Co-authored-by: mrek-msft <188900745+mrek-msft@users.noreply.github.com>
Current behavior of FileConfigurationProvider on various types of failures at various times:
OnLoadExceptioncallback is calledAddJsonFilethrowsOnLoadExceptioncallback is calledprogram continues
no reload happen
AddJsonFilethrowsprogram continues
no reload happen
possibly raises
UnobservedTaskExceptionlaterThis PR unifies it in a way that both types of failure behave same as data parsing failure.
This brings some behavioral changes
OnLoadExceptioncallback is newly triggered if IO error happen on both types of events (first load, reload)UnobservedTaskExceptioncan no longer be observed in scenario when user registersOnLoadExceptioncallback. It can however happen when no callback is registered, so XML comment onOnLoadExceptionwas updated.OnLoadExceptioncallbacks now receives not only InvalidDataException, but also IOException (or any other exception which (possibly custom) implementation of IFileProvider can throw). If user cast to InvalidDataException, he may miss other exceptions or get cast exceptions.Based on follow up Copilot code reviews, I did few more behavior changes at edge cases, they do not relate directly to issue and can be reverted independently if not desired.
OnReload()now fires only when Data actually changed. Previously on first load, if a parse or IO error occurred andOnLoadExceptionsetIgnore = true,OnReload()was fired unconditionally even though Data was never modified. The new code firesOnReload()only when Data was successfully loaded or explicitly cleared (e.g., on reload or optional missing file). For consecutive reloads with parse errors, Data is cleared andOnReload()still fires. This change is independent on original fix and can be reverted independently if not desirable.Fix #113964