mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-07-12 13:29:04 +00:00
SharePoint 404s raise ResourceNotFoundError without error-level logging 🟥
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
87ac00d499
commit
4a71c555fa
3 changed files with 62 additions and 0 deletions
0
tests/utils/__init__.py
Normal file
0
tests/utils/__init__.py
Normal file
58
tests/utils/test_sharepoint_client_error_handling.py
Normal file
58
tests/utils/test_sharepoint_client_error_handling.py
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import logging
|
||||
from typing import Any, Optional
|
||||
|
||||
import pytest
|
||||
|
||||
from utils.sharepoint.sharepoint_client import (
|
||||
ResourceNotFoundError,
|
||||
api_call_decorator,
|
||||
)
|
||||
|
||||
|
||||
class _FakeResponse:
|
||||
def __init__(self, status_code: int, error_code: str, message: str) -> None:
|
||||
self.status_code = status_code
|
||||
self._error_code = error_code
|
||||
self._message = message
|
||||
self.headers: dict[str, str] = {}
|
||||
|
||||
def json(self) -> dict[str, Any]:
|
||||
return {"error": {"code": self._error_code, "message": self._message}}
|
||||
|
||||
|
||||
class _FakeClient:
|
||||
headers: Optional[dict[str, str]] = {}
|
||||
|
||||
def is_access_token_expired(self) -> bool:
|
||||
return False
|
||||
|
||||
@api_call_decorator
|
||||
def fetch(self, path: str) -> Any:
|
||||
return "GET", f"https://graph.example.invalid/root:/{path}:/children", None
|
||||
|
||||
|
||||
def _patch_response(
|
||||
monkeypatch: pytest.MonkeyPatch, response: _FakeResponse
|
||||
) -> None:
|
||||
monkeypatch.setattr(
|
||||
"utils.sharepoint.sharepoint_client.requests.request",
|
||||
lambda *args, **kwargs: response,
|
||||
)
|
||||
|
||||
|
||||
def test_404_raises_resource_not_found_without_error_logs(
|
||||
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
# Arrange
|
||||
_patch_response(
|
||||
monkeypatch,
|
||||
_FakeResponse(404, "itemNotFound", "The resource could not be found."),
|
||||
)
|
||||
|
||||
# Act
|
||||
with pytest.raises(ResourceNotFoundError):
|
||||
_FakeClient().fetch("missing/folder")
|
||||
|
||||
# Assert
|
||||
error_records = [r for r in caplog.records if r.levelno >= logging.ERROR]
|
||||
assert error_records == []
|
||||
|
|
@ -21,6 +21,10 @@ from utils.logger import setup_logger
|
|||
logger = setup_logger()
|
||||
|
||||
|
||||
class ResourceNotFoundError(ValueError):
|
||||
"""Raised when the API returns 404 — an expected miss, not a failure."""
|
||||
|
||||
|
||||
def handle_error(response):
|
||||
"""
|
||||
Handle errors based on HTTP status codes and log detailed information.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue