Skip to content

Fix #1722: Normalize folder not found errors to DirectoryNotFoundException#1725

Merged
Adam-it merged 2 commits intopnp:devfrom
ejazhussain:fix/1722-getfolder-exception
Feb 7, 2026
Merged

Fix #1722: Normalize folder not found errors to DirectoryNotFoundException#1725
Adam-it merged 2 commits intopnp:devfrom
ejazhussain:fix/1722-getfolder-exception

Conversation

@ejazhussain
Copy link
Copy Markdown
Contributor

Fixes #1722

Summary

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 PR 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 with offline mock data

Root 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

  • ✅ Tests run offline using mock data
  • ✅ Code follows existing patterns (ErrorIndicatesFileDoesNotExists)
  • ✅ Mocking enabled for CI/CD compatibility

…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
Copy link
Copy Markdown
Member

@Adam-it Adam-it left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Image

and with your fix I now get

Image

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.

@jansenbe
Copy link
Copy Markdown
Contributor

jansenbe commented Feb 2, 2026

@ejazhussain: Nice work on this PR! @Adam-it : thanks for reviewing it.

@Adam-it Adam-it self-assigned this Feb 2, 2026
Copy link
Copy Markdown
Member

@Adam-it Adam-it left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready to merge 🚀

@Adam-it Adam-it merged commit 8e347b2 into pnp:dev Feb 7, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pnpContext.Web.GetFolder* methods throws the wrong error

3 participants