Coverage for netrun_errors / resource.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-12-15 18:37 +0000

1""" 

2Resource-related exceptions for Netrun Systems. 

3 

4Exceptions for resource lifecycle operations (CRUD) including 

5not found and conflict scenarios. 

6""" 

7 

8from typing import Any, Dict, Optional 

9 

10from fastapi import status 

11 

12from .base import NetrunException 

13 

14 

15class ResourceNotFoundError(NetrunException): 

16 """ 

17 Raised when requested resource does not exist. 

18 

19 Status Code: 404 Not Found 

20 Error Code: RESOURCE_NOT_FOUND 

21 """ 

22 

23 def __init__( 

24 self, 

25 resource_type: str = "Resource", 

26 resource_id: Optional[str] = None, 

27 message: Optional[str] = None, 

28 details: Optional[Dict[str, Any]] = None, 

29 correlation_id: Optional[str] = None, 

30 ): 

31 if message is None: 

32 if resource_id: 

33 message = f"{resource_type} with ID '{resource_id}' not found" 

34 else: 

35 message = f"{resource_type} not found" 

36 

37 if details is None: 

38 details = {} 

39 if resource_id: 

40 details["resource_id"] = resource_id 

41 details["resource_type"] = resource_type 

42 

43 super().__init__( 

44 status_code=status.HTTP_404_NOT_FOUND, 

45 error_code="RESOURCE_NOT_FOUND", 

46 message=message, 

47 details=details, 

48 correlation_id=correlation_id, 

49 ) 

50 

51 

52class ResourceConflictError(NetrunException): 

53 """ 

54 Raised when resource operation conflicts with existing state. 

55 

56 Common scenarios: 

57 - Duplicate resource creation (unique constraint violation) 

58 - Concurrent modification conflicts 

59 - State transition violations 

60 

61 Status Code: 409 Conflict 

62 Error Code: RESOURCE_CONFLICT 

63 """ 

64 

65 def __init__( 

66 self, 

67 message: str = "Resource operation conflicts with existing state", 

68 details: Optional[Dict[str, Any]] = None, 

69 correlation_id: Optional[str] = None, 

70 ): 

71 super().__init__( 

72 status_code=status.HTTP_409_CONFLICT, 

73 error_code="RESOURCE_CONFLICT", 

74 message=message, 

75 details=details, 

76 correlation_id=correlation_id, 

77 )