Fix #1722: Normalize folder not found errors to DirectoryNotFoundException#1725
Merged
Fix #1722: Normalize folder not found errors to DirectoryNotFoundException#1725
Conversation
…xception When GetFolderByServerRelativeUrlAsync and GetFolderByIdAsync are called with a non-existent folder, SharePoint REST API returns error code -2147024894 (ERROR_FILE_NOT_FOUND) with type "System.IO.FileNotFoundException". This is misleading since the resource is a folder, not a file. This fix adds context-aware error normalization in the GetFolder* methods to change the error code to "System.IO.DirectoryNotFoundException" and the message to "Folder not found." when the folder doesn't exist. Changes: - Added ErrorIndicatesFolderDoesNotExists helper in Folder.cs to detect folder-not-found errors (HTTP 404 + ServerErrorCode -2147024894) - Modified GetFolderByServerRelativeUrlAsync and GetFolderByIdAsync in Web.cs to normalize error code and message for missing folders - Added two test methods to verify correct exception handling - Included mock data files for offline test execution
1 task
Adam-it
reviewed
Feb 1, 2026
Member
Adam-it
left a comment
There was a problem hiding this comment.
@ejazhussain I had a quick look at your PR and checked it locally and it seems to be working perfectly
Without your change I was able to reproduce the error
and with your fix I now get
one tiny detail is that I would use ....Not Found with capital N and capital F so it is kinda aligned with the error message we had when it comes to Files.
Contributor
|
@ejazhussain: Nice work on this PR! @Adam-it : thanks for reviewing it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1722
Summary
When
GetFolderByServerRelativeUrlAsyncandGetFolderByIdAsyncare called with a non-existent folder, SharePoint REST API returns error code-2147024894(ERROR_FILE_NOT_FOUND) with typeSystem.IO.FileNotFoundException. This is misleading since the resource is a folder, not a file.This PR adds context-aware error normalization in the
GetFolder*methods to change the error code toSystem.IO.DirectoryNotFoundExceptionand the message to "Folder not found." when the folder doesn't exist.Changes
ErrorIndicatesFolderDoesNotExistshelper inFolder.csto detect folder-not-found errors (HTTP 404 + ServerErrorCode -2147024894)GetFolderByServerRelativeUrlAsyncandGetFolderByIdAsyncinWeb.csto normalize error code and message for missing foldersRoot Cause
SharePoint REST API uses Windows error code
0x80070002(ERROR_FILE_NOT_FOUND) for both files AND folders. This is SharePoint server behavior that cannot be changed, so the fix normalizes the error client-side in the context of folder operations.Testing
ErrorIndicatesFileDoesNotExists)