Coverage for src/csv_schema_validator/exceptions.py: 88%

40 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-12-23 15:34 +0100

1""" 

2Custom exceptions for CSV schema validation. 

3 

4This module defines specific exception classes for different types of errors 

5that can occur during CSV validation, providing better error handling and 

6more informative error messages. 

7""" 

8from __future__ import annotations 

9 

10from typing import Any 

11 

12 

13class CSVValidationError(Exception): 

14 """Base exception for CSV validation errors.""" 

15 

16 def __init__(self, message: str, row: int | None = None, column: str | None = None, 

17 value: Any | None = None, details: dict[str, Any] | None = None): 

18 super().__init__(message) 

19 self.message = message 

20 self.row = row 

21 self.column = column 

22 self.value = value 

23 self.details = details or {} 

24 

25 def to_dict(self) -> dict[str, Any]: 

26 """Convert exception to dictionary format for JSON serialization.""" 

27 return { 

28 "error_type": self.__class__.__name__, 

29 "error_message": self.message, 

30 "row": self.row, 

31 "column": self.column, 

32 "value": self.value, 

33 "details": self.details 

34 } 

35 

36 

37class SchemaValidationError(CSVValidationError): 

38 """Raised when schema validation fails.""" 

39 

40 def __init__(self, message: str, field_path: str | None = None, 

41 details: dict[str, Any] | None = None): 

42 super().__init__(message, details=details) 

43 self.field_path = field_path 

44 

45 

46# Field validation exceptions are now in csv-field-validators package 

47# Re-export for backward compatibility 

48from .field_validators import ( 

49 EnumValidationError, 

50 FieldValidationError, 

51 PatternValidationError, 

52 RangeValidationError, 

53 RequiredFieldError, 

54 TypeValidationError, 

55) 

56 

57 

58class FileError(CSVValidationError): 

59 """Raised when there are file-related errors.""" 

60 

61 def __init__(self, message: str, file_path: str, details: dict[str, Any] | None = None): 

62 super().__init__(message, details=details) 

63 self.file_path = file_path 

64 

65 def to_dict(self) -> dict[str, Any]: 

66 """Convert exception to dictionary format for JSON serialization.""" 

67 result = super().to_dict() 

68 result["details"]["file_path"] = self.file_path 

69 return result 

70 

71 

72class CSVFileError(FileError): 

73 """Raised when there are CSV file-specific errors.""" 

74 

75 def __init__(self, message: str, file_path: str, details: dict[str, Any] | None = None): 

76 super().__init__(message, file_path, details) 

77 

78 

79class SchemaFileError(FileError): 

80 """Raised when there are schema file-specific errors.""" 

81 

82 def __init__(self, message: str, file_path: str, details: dict[str, Any] | None = None): 

83 super().__init__(message, file_path, details) 

84 

85 

86class EmptyFileError(FileError): 

87 """Raised when a file is empty.""" 

88 

89 def __init__(self, file_path: str, file_type: str = "file"): 

90 message = f"{file_type.title()} file is empty" 

91 super().__init__(message, file_path, details={"file_type": file_type}) 

92 

93 

94class InvalidJSONError(SchemaFileError): 

95 """Raised when schema file contains invalid JSON.""" 

96 

97 def __init__(self, file_path: str, json_error: str): 

98 message = f"Invalid JSON in schema file: {json_error}" 

99 super().__init__(message, file_path, details={"json_error": json_error}) 

100 

101 

102# ValidationConfigurationError is now in csv-field-validators package 

103from .field_validators import ValidationConfigurationError